带输出的sqlserver游标(统计各表的数量)

declare @count int
declare @total int
set @total=0
declare @tablename varchar(100)
declare @sql nvarchar(4000)
declare mycursor cursor for
select name from sysobjects where name like 'table____' 
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)
		set @total = @total + @count
	begin
		print @tablename
		print @count
		
	end
	
fetch next from mycursor into @tablename	
end
	print @total
close mycursor
deallocate mycursor

猜你喜欢

转载自pepple.iteye.com/blog/2164128