Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

For, SQL SERVER, T-SQL,SSRS,Crystal Reports,SSIS
Browse by Tags · View All
SQL Server 6
#SQLServer 4
Database 3
#TSQL 3
TSQL 3
T-Sql. 2
Indexes 1

Archive · View All
May 2012 2
October 2011 2
December 2012 1
September 2012 1
March 2012 1
December 2011 1

Shivendra Kumar's Blog

Find table names for a specific valued column in a database

Mar 17 2012 5:50PM by Shivendra Kumar Yadav   

Few days before i have a requirement to find out the table name which are having a specific column with a specific value throughout the database.

for that i build a query which takes  <COLUMN NAME> and <VALUE> as parameter 

For example : A column <Col1> occurs in multiple tables in a database and we have to find out  those tables name which are having <Col1>

with a perticular value (say <Val1>)  than this procedure will help.

Set ansi_nulls on

go

set quoted_identifier on

go

 

 

 

create Procedure [dbo].[TableIdentifierForSpecificValue]

                                                                                                (

                                                                                                  @ColumnName Varchar(100)

                                                                                                , @Value varchar(100) 

                                                                                                )

as

begin

 

DECLARE  @Query varchar(max)

       , @Count int = 1

     

IF OBJECT_ID('Tempdb..#Imd') is not null

drop table #Imd

 

IF OBJECT_ID('Tempdb..#OutPut') is not null

drop table #OutPut

 

CREATE TABLE #IMD (ID INT, QUERY VARCHAR(MAX),TableName varchar(100), TableSchema varchar(100))

CREATE TABLE #OutPut (TableName Varchar(100))

 

INSERT INTO #IMD (ID,QUERY,TableName,TableSchema)

SELECT  ROW_NUMBER() OVER (ORDER BY CLM.TABLE_CATALOG)

       , 'SELECT ' + CLM.COLUMN_NAME + ' FROM ' + TBL.TABLE_SCHEMA + '.' + CLM.TABLE_NAME + ' WHERE Convert(Varchar(100),' + CLM.COLUMN_NAME + ') = ''' + @Value + ''''

       , CLM.TABLE_NAME , TBL.TABLE_SCHEMA

FROM  Information_Schema.COLUMNS CLM

INNER JOIN INFORMATION_SCHEMA.TABLES TBL ON CLM.COLUMN_NAME = @ColumnName

                                        AND TBL.TABLE_TYPE = 'BASE TABLE'

                                        AND CLM.TABLE_NAME = TBL.TABLE_NAME

                                   

 

WHILE @Count <= (SELECT COUNT(1) FROM #IMD)

BEGIN

set @Query = '

IF EXISTS ( ' + (SELECT QUERY FROM #IMD WHERE ID = @COUNT )+ ')

 SELECT ''' + (SELECT TableSchema + '.' + TableName FROM #IMD WHERE ID = @COUNT ) + ''''

 

INSERT INTO #OutPut (TableName)

EXEC(@query)

 

SET @Count = @Count + 1

 

END

 

SELECT * FROM #OutPut

 

IF OBJECT_ID('Tempdb..#Imd') is not null

drop table #Imd

 

IF OBJECT_ID('Tempdb..#OutPut') is not null

drop table #OutPut

 

end

 

 

 

Tags: TSQL, #SQLServer, #TSQL, SQL Server, Database,


Shivendra Kumar Yadav
64 · 3% · 892
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Find table names for a specific valued column in a database" rated 5 out of 5 by 1 readers
Find table names for a specific valued column in a database , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]