有点卡住,一直在玩sql查找表,每个表的计数超过1000行,名称长度超过20个字符等,但我已经空白了。
这是我当前查找长度超过20个字符的条目,我需要对其进行修改以显示其中包含10个以上列的表。
(我需要改变的位置在底部)
USE DW_Foundation
DECLARE @TableName varchar(255)
Drop Table #Temp
Create Table #Temp (TableName Varchar(50), [Count] bigint)
DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table' and table_name like 'BDR_%'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
Declare @SQL Varchar(max)
Set @SQL = 'Insert Into #Temp'
Set @SQL = @SQL + ' Select ''' + @TableName + ''' as [table], count(1)as count from ' + @TableName
EXEC (@SQL)
FETCH NEXT FROM TableCursor INTO @TableName
END
Select * from #Temp ((This bit here, where...?))
CLOSE TableCursor
DEALLOCATE TableCursor
答案 0 :(得分:1)
游标不是一个很好的方法......
您还应该使用sys
表而不是INFORMATION_SCHEMA
。
以下内容将获取名称长度超过20个字符且超过10列的所有表格:
SELECT t.name, COUNT(*) AS columns
FROM sys.columns AS c
INNER JOIN sys.tables AS t
ON t.object_id = c.object_id
WHERE LEN(t.name) > 20
GROUP BY t.name
HAVING COUNT(*) > 10;