我有2个查询,其中一个查询包含另一个查询要使用的参数。我做了类似的事情,但没有多个参数。以下是查询:
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
所以如果第一个查询的结果是:
[parameter] [value]
targetDatabaseName SomeDataBaseName
targetDatasetSchema data
targetDatasetName TheTableName
第二个查询中的where子句将使用这些参数:
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'SomeDataBaseName'
AND TABLE_SCHEMA = 'data'
AND TABLE_NAME = 'TheTableName'
我想按表名对所有内容进行分组,以便将其导出为CSV。我尝试了一些事情,但我无法自豪地说会有所作为。
答案 0 :(得分:0)
如果你真的不需要数据库,那么你可以使用类似的东西:
with FirstQuery as (
select max(case when parameter = 'TargetDatabaseName' then value end) as DB,
max(case when parameter = 'TargetDatasetSchema' then value end) as Schema,
max(case when parameter = 'TargetDataSetName' then value end) as table
from (<first query goes here>) t
)
selelect <whatever>
from Information_Schema q2 join
FirstQuery q1
on q1.Table_Schema = q2.Schema and
q1.Table_Name = q2.table
我看到的问题是数据库。在SQL Server中,Information_Schema表仅包含有关一个数据库的信息。要一次获取多个数据库,您可能必须使用动态SQL。