Wednesday, July 20, 2011

Query in sql to find the names of table having some particular column with some particular data and also find the number of appearances of data

DECLARE @table_name VARCHAR(1000) -- table name Declare @COUNT VARCHAR(8000) -- count of the occurence of emp_id
DECLARE db_cursor CURSOR FOR select table_name from INFORMATION_SCHEMA.COLUMNSwhere COLUMN_NAMe like '%INT_EMP_ID%'
OPEN
db_cursor FETCH NEXT FROM db_cursor INTO @table_nameWHILE @@FETCH_STATUS = 0 BEGIN
SET @COUNT = 'select count(*) AS ' +@table_name+ ' from ' + @table_name + ' where' + ' INT_EMP_ID = 222201787 'EXEC(@COUNT)
FETCH NEXT FROM db_cursor INTO @table_name
END
CLOSE db_cursor
DEALLOCATE db_cursor

0 comments:

Post a Comment