Advertisement
bluebunny72

Find U/M data by UserDefinedType

Feb 12th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.26 KB | None | 0 0
  1. --run with output set to Results To Text, copy and paste first results into new window to get the second results, etc.
  2. SET NOCOUNT ON
  3. DECLARE @UM VARCHAR(500)='EA'
  4. SELECT 'IF EXISTS (SELECT TOP 1 1 FROM ' + col.TABLE_NAME  + ' WHERE ' +  col.COLUMN_NAME + ' = ''' + @UM + ''') PRINT ''SELECT ' + col.COLUMN_NAME + ' FROM ' + col.TABLE_NAME + ' WHERE ' + col.COLUMN_NAME + ' = ''''' + @UM + ''''' '';'
  5. FROM INFORMATION_SCHEMA.COLUMNS col
  6. JOIN information_schema.TABLES tbl
  7.        ON tbl.TABLE_NAME = col.TABLE_NAME
  8.           AND tbl.table_schema = col.table_schema
  9.           AND tbl.table_catalog = col.table_catalog
  10.           AND tbl.table_type <> 'VIEW'
  11. WHERE DOMAIN_NAME = 'UMType'
  12. ORDER BY col.TABLE_NAME
  13.  
  14. SELECT 'IF EXISTS (SELECT TOP 1 1 FROM ' + col.TABLE_NAME  + ' WHERE ' +  col.COLUMN_NAME + ' = ''' + @UM + ''') PRINT ''SELECT ' + col.COLUMN_NAME + ' FROM ' + col.TABLE_NAME + ' WHERE ' + col.COLUMN_NAME + ' = ''''' + @UM + ''''' '';'
  15. FROM INFORMATION_SCHEMA.COLUMNS col
  16. JOIN information_schema.TABLES tbl
  17.        ON tbl.TABLE_NAME = col.TABLE_NAME
  18.           AND tbl.table_schema = col.table_schema
  19.           AND tbl.table_catalog = col.table_catalog
  20.           AND tbl.table_type <> 'VIEW'
  21. WHERE DOMAIN_NAME = 'WeightUnitsType'
  22. ORDER BY col.TABLE_NAME
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement