Search column name having specific value in tables in certain database

时光怂恿深爱的人放手 提交于 2019-12-06 02:59:08

This modification to your sql will generate more SQL that you can run

SELECT 
    'select '''+SCHEMA_NAME(schema_id) +'.'+t.name + ''', '+c.name + ' from ' 
      + SCHEMA_NAME(schema_id) +'.' + t.name
      + ' where ' + c.name + ' = 35'

FROM sys.tables AS t 
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID 
WHERE c.name LIKE '%ID%' 
ORDER BY schema_name(schema_id), t.name

You could make the SQL run automatically if necessary, but the syntax for that depends on your platform. Alternatively, copy the results into your query tool and run them, if that suffices

select s.name, c.name from sys.columns c inner join sys.tables s on s.object_id = c.object_id where c.name like'%35%'

This will give you the columns with 35 and the associated table.

declare @tableName varchar(100), @colName varchar(100) declare @sqlStatement nvarchar(100)

declare tablelist cursor for select s.name, c.name from sys.columns c inner join sys.tables s on s.object_id = c.object_id where c.name like'%YourSearchCondtion%' OPEN tablelist FETCH NEXT FROM tablelist into @tableName, @colName

while @@FETCH_STATUS = 0 BEGIN

SELECT @sqlStatement = 'SELECT ' + @colName + ', * FROM ' + @tableName + ' WHERE ' + @colName + ' NOT LIKE ''%35%'''

exec sp_executesql @sqlStatement

-- Here you can get the table that you dont want and add to a temp table.. PRINT CAST(@@ROWCOUNT AS VARCHAR)

FETCH NEXT FROM tablelist INTO @tableName, @colName END

CLOSE tablelist DEALLOCATE tablelist GO

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!