直接运行sql查询输出

时间:2017-02-22 12:57:19

标签: sql-server sql-server-2008

我有一组查询可以在不同的表中添加4个不同的列,但名称有些相似。因此,我通过在一组动态sql中调用INFORMATION_SCHEMA.COLUMNS来建立所有4个查询的联合,这些动态sql具有所有表的名称和所有4列。 现在我想一次执行所有这些查询,而不是逐个执行每个查询。那我怎么能实现呢?

这是我的查询:

SELECT * FROM (SELECT
'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' + TABLE_NAME + ''' AND COLUMN_NAME = ''FromDays'') ALTER TABLE '+ TABLE_NAME +' ADD FromDays varchar(50) NULL; ' AS qry
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%' 
UNION ALL
SELECT
'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' + TABLE_NAME + ''' AND COLUMN_NAME = ''ToDays'') ALTER TABLE '+ TABLE_NAME +' ADD ToDays varchar(50) NULL; ' AS qry
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%' 
UNION ALL
SELECT
'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' + TABLE_NAME + ''' AND COLUMN_NAME = ''FromDays_Top'') ALTER TABLE '+ TABLE_NAME +' ADD FromDays_Top varchar(50) NULL; ' AS qry
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%' 
UNION ALL
SELECT
'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' + TABLE_NAME + ''' AND COLUMN_NAME = ''ToDays_Top'') ALTER TABLE '+ TABLE_NAME +' ADD ToDays_Top varchar(50) NULL; ' AS qry
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%') x

这给了我输出为: output

1 个答案:

答案 0 :(得分:2)

DECLARE @SQL NVARCHAR(MAX);
SELECT @SQL = (
    (
        SELECT
        'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' + TABLE_NAME + ''' AND COLUMN_NAME = ''MyColumn1'') ALTER TABLE '+ TABLE_NAME +' ADD MyColumn1 varchar(50) NULL; ' AS qry
        FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_NAME LIKE '%myTable%'
    )
    + CHAR(13) +
    (
        SELECT
        'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' + TABLE_NAME + ''' AND COLUMN_NAME = ''MyColumn2'') ALTER TABLE '+ TABLE_NAME +' ADD MyColumn2 varchar(50) NULL; ' AS qry
        FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_NAME LIKE '%myTable%'
    )
    + CHAR(13) +
    (
        SELECT
        'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' + TABLE_NAME + ''' AND COLUMN_NAME = ''MyColumn3'') ALTER TABLE '+ TABLE_NAME +' ADD MyColumn3 varchar(50) NULL; ' AS qry
        FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_NAME LIKE '%myTable%'
    )
)
;
SELECT @SQL
--EXECUTE SP_ExecuteSQL @SQL
;

如果结果符合您的喜好,请移除底部的SELECT @SQL并取消注释EXECUTE SP_ExecuteSQL @SQL以执行命令。

首先编辑:

好的,我为误解了问题的背景而道歉。这样的事情怎么样:

DECLARE @SQL NVARCHAR(MAX);
SELECT @SQL = REPLACE(SUBSTRING(
    (
    SELECT CHAR(10) 
    + 'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' 
    + TABLE_NAME 
    + ''' AND COLUMN_NAME = ''FromDays'') ALTER TABLE '
    + TABLE_NAME 
    +' ADD FromDays varchar(50) NULL; '
    FROM INFORMATION_SCHEMA.Tables
    WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%'
    ORDER BY TABLE_NAME
    FOR XML PATH('')
    )
,1,4000),'#x0D',CHAR(10))
    +
    REPLACE(SUBSTRING(
        (
        SELECT CHAR(10) 
        + 'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' 
        + TABLE_NAME 
        + ''' AND COLUMN_NAME = ''ToDays'') ALTER TABLE '
        + TABLE_NAME 
        +' ADD ToDays varchar(50) NULL; '
        FROM INFORMATION_SCHEMA.Tables
        WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%'
        ORDER BY TABLE_NAME
        FOR XML PATH('')
        )
    ,1,4000),'#x0D',CHAR(10))
    +
    REPLACE(SUBSTRING(
        (
        SELECT CHAR(10) 
        + 'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' 
        + TABLE_NAME 
        + ''' AND COLUMN_NAME = ''FromDays_Top'') ALTER TABLE '
        + TABLE_NAME 
        +' ADD FromDays_Top varchar(50) NULL; '
        FROM INFORMATION_SCHEMA.Tables
        WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%'
        ORDER BY TABLE_NAME
        FOR XML PATH('')
        )
    ,1,4000),'#x0D',CHAR(10))
    +
        REPLACE(SUBSTRING(
        (
        SELECT CHAR(10) 
        + 'IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =''' 
        + TABLE_NAME 
        + ''' AND COLUMN_NAME = ''ToDays_Top'') ALTER TABLE '
        + TABLE_NAME 
        +' ADD ToDays_Top varchar(50) NULL; '
        FROM INFORMATION_SCHEMA.Tables
        WHERE TABLE_NAME LIKE '%tbl_PPT_Query_Order%'
        ORDER BY TABLE_NAME
        FOR XML PATH('')
        )
    ,1,4000),'#x0D',CHAR(10))
SELECT @SQL
--EXECUTE SP_ExecuteSQL @SQL
;