我有一个700行的表。我想要做的是,执行`select * from table_name'对它进行查询以及我将要将其存储在变量中的任何结果,在完成之后,是否要遍历每个记录以进行处理?我该如何实现?任何帮助?
谢谢你, -saurabh
答案 0 :(得分:1)
你想要一些名为cursors
光标
使用游标来获取查询返回的行。您可以使用查询将行检索到游标中,然后从游标中一次获取一行。
步骤
希望这可以帮助你cursor
答案 1 :(得分:0)
这是我用
开头的例子USE pubs
GO
-- Declare the variables to store the values returned by FETCH.
DECLARE @au_lname varchar(40), @au_fname varchar(20)
DECLARE authors_cursor CURSOR FOR
SELECT au_lname, au_fname FROM authors
--WHERE au_lname LIKE 'B%'
ORDER BY au_lname, au_fname
OPEN authors_cursor
-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.
FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- Concatenate and display the current values in the variables.
PRINT @au_fname
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO