Page 118 - CITS - Computer Software Application -TT
P. 118

COMPUTER SOFTWARE APPLICATION - CITS



































             Using Cursor


           Implementation of Example
           Let us implement the cursor example in the SQL server. We can do this by first creating a table named “customer”
           using the below statement:
           1  CREATE TABLE customer (
           2  id int PRIMARY KEY,
           3  c_name nvarchar(45) NOT NULL,
           4  email nvarchar(45) NOT NULL,
           5  city nvarchar(25) NOT NULL
           6  );
           Next, we will insert values into the table. We can execute the below statement to add data into a table:
           1  INSERT INTO customer (id, c_name, email, city)
           2  VALUES (1,’Steffen’, ‘stephen@javatpoint.com’, ‘Texas’),
           3  (2, ‘Joseph’, ‘Joseph@javatpoint.com’, ‘Alaska’),
           4  (3, ‘Peter’, ‘Peter@javatpoint.com’, ‘California’),
           5  (4,’Donald’, ‘donald@javatpoint.com’, ‘New York’),
           6  (5, ‘Kevin’, ‘kevin@javatpoint.com’, ‘Florida’),
           7  (6, ‘Marielia’, ‘Marielia@javatpoint.com’, ‘Arizona’),
           8  (7,’Antonio’, ‘Antonio@javatpoint.com’, ‘New York’),
           9  (8, ‘Diego’, ‘Diego@javatpoint.com’, ‘California’);
           We can verify the data by executing the SELECT statement:
           1  SELECT * FROM customer;
           After executing the query, we can see the below output where we have eight rows into the table:
           Now, we will create a cursor to display the customer records. The below code snippets explain the all steps of the
           cursor declaration or creation by putting everything together:
           1  --Declare the variables for holding data.
           2  DECLARE @id INT, @c_name NVARCHAR(50), @city NVARCHAR(50)
           3




                                                           105

                              CITS : IT&ITES - Computer software application - Lesson 18 - 36
   113   114   115   116   117   118   119   120   121   122   123