SQL 存儲過程里讀取表內容 游標fetch的使用


DECLARE @Hid varchar(50), @Hname varchar(50)
declare contact_cursor cursor for --聲明游標
select hotel_id,h_name from hotel where hotel_id<25

open contact_cursor --打開游標
fetch next from contact_cursor into @Hid,@Hname --游標指針下移一行
while @@fetch_status=0 --FETCH語句執行成功
begin
    print @Hid + @Hname
    fetch next from contact_cursor into @Hid,@Hname
end
CLOSE contact_cursor --關閉游標
DEALLOCATE contact_cursor --釋放游標
fetch next from contact_cursor into @Hid,@Hname
這段語句就是把 hotel_id 和 h_name 字段賦值給 @Hid,@Hname
print @Hid + @Hname
就是把hotel表里的hotel_id和h_name字段打印出來



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM