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’, ‘[email protected]’, ‘Texas’),
           3  (2, ‘Joseph’, ‘[email protected]’, ‘Alaska’),
           4  (3, ‘Peter’, ‘[email protected]’, ‘California’),
           5  (4,’Donald’, ‘[email protected]’, ‘New York’),
           6  (5, ‘Kevin’, ‘[email protected]’, ‘Florida’),
           7  (6, ‘Marielia’, ‘[email protected]’, ‘Arizona’),
           8  (7,’Antonio’, ‘[email protected]’, ‘New York’),
           9  (8, ‘Diego’, ‘[email protected]’, ‘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