declare @tablename nvarchar(20)
declare @fieldname nvarchar(50)
declare @SQLString nvarchar(300)
DECLARE tnames_cursor CURSOR LOCAL FORWARD_ONLY READ_ONLY--声明游标
FOR select name from sysobjects where type='U';--游标需要用到的列
open tnames_cursor--打开游标
FETCH NEXT FROM tnames_cursor INTO @tablename --移动游标给变量赋值,应该与游标需要用到的列一一对应 ,顺序类型应该一致
WHILE (@@FETCH_STATUS = 0)--循环
BEGIN
BEGIN
set @fieldname= @tablename+'_TableCount'
-- SET @SQLString = N'select cast(count(*) as char(10))+ ''' +@fieldname +''' as '+@fieldname +' from ' +@tablename +' having count(*)>1';
SET @SQLString = N'select cast(count(*) as char(10))+ ''' +@tablename +''' as '+@fieldname +' from ' +@tablename +' having count(*)>1';
EXECUTE dbo.sp_executesql @SQLString
END
FETCH NEXT FROM tnames_cursor INTO @tablename --移动游标 给变量赋值 应该与游标需要用到的列一一对应 ,顺序类型应该一致
END
CLOSE tnames_cursor--关闭游标
DEALLOCATE tnames_cursor--释放游标
declare @tablename nvarchar(20) declare @SQLString nvarchar(300) DECLARE @IntTotalCount int; DECLARE @ParmDefinition nvarchar(500); SET @ParmDefinition = N' @TotalCountOut int OUTPUT'; DECLARE tnames_cursor CURSOR LOCAL FORWARD_ONLY READ_ONLY--声明游标 FOR select name from sysobjects where type='U';--游标需要用到的列 open tnames_cursor--打开游标 FETCH NEXT FROM tnames_cursor INTO @tablename --移动游标给变量赋值,应该与游标需要用到的列一一对应 ,顺序类型应该一致 WHILE (@@FETCH_STATUS = 0)--循环 BEGIN BEGIN SET @SQLString = N'select @TotalCountOut=count(*) from ' +@tablename +' having count(*)>1'; EXECUTE dbo.sp_executesql @SQLString ,@ParmDefinition, @TotalCountOut=@IntTotalCount OUTPUT; if(@IntTotalCount>0) begin print @tablename+' has '+cast(@IntTotalCount as char(10)) print N'select * from '+ +@tablename end END FETCH NEXT FROM tnames_cursor INTO @tablename --移动游标 给变量赋值 应该与游标需要用到的列一一对应 ,顺序类型应该一致 END CLOSE tnames_cursor--关闭游标 DEALLOCATE tnames_cursor--释放游标