游标示例

游标示例 代码
   
     
SET NOCOUNT ON

begin tran

DECLARE @newsid varchar ( 80 )
DECLARE @ishaveAnnex varchar ( 100 )

DECLARE vendor_cursor CURSOR FOR
SELECT [ newsid ] FROM [ News ]

OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor INTO @newsid

WHILE @@FETCH_STATUS = 0
BEGIN

select @ishaveAnnex = ishaveAnnex from News where newsid = @newsid

if ( @ishaveAnnex = ' 1 ' )
begin
update News set ishaveAnnex = ' 0 ' where newsid = @newsid
end
else if ( @ishaveAnnex = ' 0 ' )
begin
update News set ishaveAnnex = ' 1 ' where newsid = @newsid
end
FETCH NEXT FROM vendor_cursor INTO @newsid

END
CLOSE vendor_cursor
DEALLOCATE vendor_cursor
if @@error <> 0
rollback tran
else
commit tran

 

你可能感兴趣的:(游标)