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字段打印出來