查询当前数据库中所有用户表中哪些字段包含某个字符串
1个回答
展开全部
CREATE PROCEDURE FindString(@Value VARCHAR(1024)) AS
BEGIN
SET NOCOUNT ON;
搜团DECLARE @SQL VARCHAR(6000)
DECLARE @Table VARCHAR(100)
DECLARE @Column VARCHAR(100)
CREATE TABLE #T(TableName VARCHAR(100),ColumnName VARCHAR(100))
DECLARE TABLES CURSOR FOR SELECT O.name,C.name FROM sys.syscolumns C INNER JOIN sys.sysobjects O ON C.id=O.id
WHERE O.type='U' AND C.xtype IN(167,175,231,239) ORDER BY O.name,C.name
OPEN TABLES
FETCH NEXT FROM TABLES INTO @Table, @Column
WHILE @@FETCH_STATUS = 0
BEGIN
SET 念野@SQL='IF EXISTS(SELECT NULL FROM ['+@Table+'] WHERE RTRIM(LTRIM(['+@Column+'])) LIKE ''%'+@Value+'%'')'
SET @SQL=@SQL+'INSERT INTO #T VALUES ('''+@Table+''','''+@Column+''')'
EXEC(@SQL)
FETCH NEXT FROM TABLES INTO @Table,@Column
世高橘 END
CLOSE TABLES
DEALLOCATE TABLES
SELECT * FROM #T
DROP TABLE #T
End
调用方法:
EXEC FindString '查找的字符串'
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询