Collections:
"FETCH" - Transferring Data from Cursors to Variables in SQL Server
How To Transfer Data from a Cursor to Variables with a "FETCH" Statement in SQL Server Transact-SQL?
✍: FYIcenter.com
By 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 FyiCenterData; GO DECLARE fyi_cursor CURSOR FOR SELECT id, url, notes, counts, time FROM fyi_links; OPEN fyi_cursor; DECLARE @id INT, @url VARCHAR(80), @notes VARCHAR(80), @counts INT, @time DATETIME; FETCH NEXT FROM fyi_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 fyi_cursor; DEALLOCATE fyi_cursor; GO id = 101 url = dev.fyicenter.com notes = NULL counts = 0 time = Jan 1 2007 12:00AM
⇒ "@@FETCH_STATUS" - Looping through Result Set in SQL Server
⇐ "FETCH" - Fetching the Next Row from a Cursor in SQL Server
2016-10-17, 2103🔥, 0💬
Popular Posts:
What Are the Differences between DATE and TIMESTAMP in Oracle? The main differences between DATE and...
How To Query Tables and Loop through the Returning Rows in MySQL? The best way to query tables and l...
Where to find answers to frequently asked questions on PHP Connections and Query Execution for MySQL...
How To Create a Table Index in Oracle? If you have a table with a lots of rows, and you know that on...
How To Connect to a MySQL Server with a Port Number in MySQL? If you want to connect a MySQL server ...