How To Transfer Data from a Cursor to Variables with a "FETCH" Statement?
Submitted by: AdministratorBy default, a FETCH statement will display the fetched row on the client program window. If you want to transfer the output data to variables, you can specify an INTO clause with a list of variables that matches the list of fields in the result set.
The tutorial exercise below shows you a good example of using the FETCH statement to transfer one row of output data from the result set to variables:
USE GlobalGuideLineDatabase;
GO
DECLARE ggl_cursor CURSOR FOR
SELECT id, url, notes, counts, time FROM ggl_links;
OPEN ggl_cursor;
DECLARE @id INT, @url VARCHAR(80), @notes VARCHAR(80),
@counts INT, @time DATETIME;
FETCH NEXT FROM ggl_cursor INTO @id, @url, @notes,
@counts, @time;
PRINT 'id = '+CONVERT(VARCHAR(20),ISNULL(@id,0));
PRINT 'url = '+ISNULL(@url,'NULL');
PRINT 'notes = '+ISNULL(@notes,'NULL');
PRINT 'counts = '+CONVERT(VARCHAR(20),ISNULL(@counts,0));
PRINT 'time = '+CONVERT(VARCHAR(20),ISNULL(@time,'2007'));
CLOSE ggl_cursor;
DEALLOCATE ggl_cursor;
GO
id = 101
url = globalguideline.com
notes = NULL
counts = 0
time = Jan 1 2007 12:00AM
Submitted by: Administrator
The tutorial exercise below shows you a good example of using the FETCH statement to transfer one row of output data from the result set to variables:
USE GlobalGuideLineDatabase;
GO
DECLARE ggl_cursor CURSOR FOR
SELECT id, url, notes, counts, time FROM ggl_links;
OPEN ggl_cursor;
DECLARE @id INT, @url VARCHAR(80), @notes VARCHAR(80),
@counts INT, @time DATETIME;
FETCH NEXT FROM ggl_cursor INTO @id, @url, @notes,
@counts, @time;
PRINT 'id = '+CONVERT(VARCHAR(20),ISNULL(@id,0));
PRINT 'url = '+ISNULL(@url,'NULL');
PRINT 'notes = '+ISNULL(@notes,'NULL');
PRINT 'counts = '+CONVERT(VARCHAR(20),ISNULL(@counts,0));
PRINT 'time = '+CONVERT(VARCHAR(20),ISNULL(@time,'2007'));
CLOSE ggl_cursor;
DEALLOCATE ggl_cursor;
GO
id = 101
url = globalguideline.com
notes = NULL
counts = 0
time = Jan 1 2007 12:00AM
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | What Happens If NULL Values Are Involved in Bitwise Operations? |
☺ | PHP MSSQL - How To Display a Past Time in Days, Hours and Minutes? |
☺ | How to download Microsoft SQL Server 2005 Express Edition? |
☺ | Can You Roll Back the DDL Statement in a Trigger? |
☺ | What Is a Subquery in a SELECT Query Statement in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Sybase Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |