Friday, January 31, 2014

Get the data from all the tables

USE AIRLINE --DB NAME
GO
DECLARE @COLUMNNAME AS VARCHAR(50) = 'PNAME' --THE NAME OF THE COLUMN ON WHICH YOU NEED TO PUT THE CRITERIA
DECLARE @CRITERIA AS VARCHAR(50) = 'CONVERT(DATE,' + @COLUMNNAME + ') >= ''20130225''' -- THE ACTUAL CRITERIA/WHERE CLAUSE OF THE QUERY

PRINT @COLUMNNAME
PRINT @CRITERIA

SELECT 
  'SELECT ''' + T.NAME + ''' AS  TABLENAME, * FROM ' + T.NAME + ' WHERE ' + @CRITERIA 
FROM 
  SYS.COLUMNS C
INNER JOIN SYS.TABLES T
  ON T.OBJECT_ID = C.OBJECT_ID   
WHERE 
  C.NAME = @COLUMNNAME

No comments:

Post a Comment