IF object_id (N ' usp_queryalltablefieldcontent ') is not nullbegindrop PROC usp_queryalltablefieldcontentendgocreate PROCEDURE usp_queryalltablefieldcontent (@queryFieldType VARCHAR (max),--type to query @queryContent VA Rchar (max),--the content to query @queryFieldName VARCHAR (max) = "--the field name to query) as begin--CREATE table name from the database query, temporary table for field name CREATE TABLE #T MPQUERYTBL (ID INT IDENTITY (1, 1), TableName varchar ($), fieldName varchar) DECLARE @strSqlSys varchar (max) SET @s Trsqlsys= ' INSERT into #TmpQueryTbl (tablename,fieldname) ' + ' SELECT sysobjects.name as TableName, syscolumns.name as Columnsname from syscolumns ' + ' INNER joins systypes on Systypes.xtype=syscolumns.xtype INNER joins sysobjects on sysobjects . id = syscolumns.id ' + ' WHERE systypes.name= ' ' [email protected]+ ' and sysobjects.xtype= ' + ' + ' U ' + ' and Syscolu Mns.name is not NULL ' IF @queryFieldName are not null and @queryFieldName <> ' Beginset @[email protected]+ ' and Sy Scolumns.name= "[email protected]+ ' Endset @[email protected]+ ' ORDER by Syscolumns.name ' EXEC (@strSqlSys)--start querying data based on the table name and field name in the staging table DECLARE @ intcount int SET @intCount = @ @rowcount--CREATE TABLE #TmpResultTbl (id int IDENTITY (1, 1), Tablenam e varchar (fieldName), strcontent varchar (max)) DECLARE @strSql varchar (max) DECLARE @tableName varchar ( DECLARE @fieldName VARCHAR (DECLARE) @intI INT = 1 while @intI <= @intCount beginse Lect @tableName =tablename, @fieldName =fieldname from #TmpQueryTbl WHERE id= @intISET @strSql = ' INSERT into #TmpResultTbl ( tablename,fieldname,strcontent) ' + ' SELECT ' + ' [email protected]+ ' + ' + ', ' + ' [email protected]+ ' + ', [' [email protected]+ '] from [' [email protected]+ '] WHERE [' [email protected]+ ']= ' + ' [email protected]+ "EXEC (@strSql) SET @intI = @intI + 1 end SELECT *from #TmpResultTbl DROP tabl E #TmpQueryTbl DROP TABLE #TmpResultTbl END
Querying the database for the same worth all tables followed by the field "stored procedure"