带输出的游标写法

declare @count int
declare @tablename varchar(100)
declare @sql nvarchar(4000)
declare mycursor cursor for
select name from sysobjects where name like 'test____'
open mycursor
fetch next from mycursor into @tablename
while(@@FETCH_STATUS=0)
begin
set @sql='select @count=COUNT(*) from ' + @tablename
exec sp_executesql @sql,N'@count int output' ,@count=@count   output  

--print @tablename
if(@count=0)
begin
print @tablename
print @count

end

fetch next from mycursor into @tablename
end
close mycursor
deallocate mycursor

猜你喜欢

转载自pepple.iteye.com/blog/2123927
今日推荐