SQL QUERES
CURSOR EXAMPLES:
1) Write the cursor program examples for select top most 100 author name from product table and print with serial no
Answer:
SET NOCOUNT ON
DECLARE @name VARCHAR(100),@COUNT INT=0
DECLARE OPENCURSOR CURSOR FOR
SELECT TOP 100 AUTHORNAME FROM PRODUCT
OPEN OPENCURSOR
FETCH NEXT FROM OPENCURSOR INTO @name
WHILE @@FETCH_STATUS=0
BEGIN
SET @COUNT=@COUNT+1
PRINT LTRIM(STR(@COUNT)+')'+ @NAME)+','
FETCH NEXT FROM OPENCURSOR INTO @name
END
CLOSE OPENCURSOR
DEALLOCATE OPENCURSOR
Output:
1)Frances Hodgson Burnett,
2)Rudyard Kipling,
3)Kenneth Grahame,
4)Colin Hawkins,Jacqui Hawkins,
5)Jane Kemp,Clare Walters,
6)Jane Kemp,Clare Walters,
7)Enid Blyton,
8)Enid Blyton,
9)Nick Butterworth,
10)Judith Kerr,
11)Judith Kerr,
12)Dr. Seuss,
13)Dr. Seuss,
14)Dr. Seuss,
15)Dr. Seuss,
16)Elizabeth Laird,John Richardson,
17)Enid Blyton,
18)Enid Blyton,
No comments:
Post a Comment