时间:2021-07-01 10:21:17 帮助过:152人阅读
查询数据库中所有表的语句 [sql] declare @tablename nchar(20) declare @fieldname nchar(50) declare @SQLString nvarchar(300) DECLARE tnames_cursor CURSOR LOCAL FORWARD_ONLY READ_ONLY--声明游标 FOR select name from sysobjects where type=U;--游
查询数据库中所有表的语句
[sql]
declare @tablename nchar(20)
declare @fieldname nchar(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='count'+ @tablename
SET @SQLString = N'select count(*) as '+@fieldname +' from ' +@tablename +' having count(*)>1000';
EXECUTE dbo.sp_executesql @SQLString
END
FETCH NEXT FROM tnames_cursor INTO @tablename --移动游标 给变量赋值 应该与游标需要用到的列一一对应 ,顺序类型应该一致
END
CLOSE tnames_cursor--关闭游标
DEALLOCATE tnames_cursor--释放游标