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