Querying the database for the same worth all tables followed by the field "stored procedure"

Source: Internet
Author: User
Tags joins

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.