I have 2 queries where one query holds the parameters to be used by another query. I have done something similar, but not with multiple parameters. Here are the queries:
SELECT [loadJob]
,[loadStep]
,[parameter]
,[value]
FROM [Admin].[admin].[LoadParameters] where loadJob like 'someJobName%' and (parameter = 'targetDatabaseName' or parameter = 'targetDatasetName' or parameter = 'targetDatasetSchema')
SELECT
TABLE_CATALOG as DB,
TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME,
ORDINAL_POSITION,
COLUMN_DEFAULT,
IS_NULLABLE,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION,
NUMERIC_PRECISION_RADIX,
NUMERIC_SCALE,
DATETIME_PRECISION,
CHARACTER_SET_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'TheDataBase' // From the [parameter] with name 'targetDatabaseName' get form the [value] column
AND TABLE_SCHEMA = 'data' // From the [parameter] with name 'targetDatasetSchema' get form the [value] column
AND TABLE_NAME = 'TableName' // From the [parameter] with name 'targetDatasetName' get form the [value] column
so if my results of the first query are:
[parameter] [value]
targetDatabaseName SomeDataBaseName
targetDatasetSchema data
targetDatasetName TheTableName
the where clause in the second query would use those parameters like so:
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'SomeDataBaseName'
AND TABLE_SCHEMA = 'data'
AND TABLE_NAME = 'TheTableName'
I would like to group everything by table name too so I can export it to a CSV. I tried a few things, but nothing I am proud to say would work.
If you really don’t need the database, then you can use something like:
The problem that I see is the database. In SQL Server, the Information_Schema tables only contain infomation about one database. To get multiple databases at once, you may have to use dynamic SQL.