sql:oracle, CURSOR

时间:2023-03-08 22:22:40
sql:oracle, CURSOR

  

Cursors
You use a cursor to fetch rows returned by a query. You retrieve the rows into the cursor using a
query and then fetch the rows one at a time from the cursor. You typically use the following five
steps when using a cursor:
1. Declare variables to store the column values for a row.
2. Declare the cursor, which contains a query.
3. Open the cursor.
4. Fetch the rows from the cursor one at a time and store the column values in the variables
declared in Step 1. You would then do something with those variables, such as display
them on the screen, use them in a calculation, and so on.
5. Close the cursor.

1.T-SQL用法三(游标和Fetch) http://www.cnblogs.com/McJeremy/archive/2008/09/26/1299818.html

use pubs
go
--1,2 declare
declare @auid char(12),@aulname varchar(20),@aufname varchar(20), @st char(2),@auinfo varchar(50)
declare auth_cur cursor for
select au_id, au_lname, au_fname, state
from authors

--3.open
open auth_cur

--4.fetch
fetch next from auth_cur into @auid,@aulname,@aufname, @st
while (@@fetch_status=0)
begin
print '作者编号: '+@auid
print '作者姓名: '+@aulname+','+@aufname
print '所在州: '+@st
print '--------------------------'
fetch next from auth_cur into @auid,@aulname,@aufname, @st
end --5,6.close, deallocate
close auth_cur
deallocate auth_cur