Dynamically decide the table name in execute SQL task for CREATE TABLE statement

末鹿安然 提交于 2019-12-11 04:59:00

问题


I want to know if this is possible to do in SSIS Execute SQL task. I want to check for existence of a table, and if it doesn't exist, create the table. I want to keep the table name in a variable so I can execute the task against different databases and be able to change the table name.

As an example I have this code:

if not exists (select * from sysobjects where name='Table1' and xtype='U')
    Create Table Table1(
        Col11 varchar(10) primary key,
        Col12 varchar(10)
    );

For flexibility I want to store the value Table1 in a variable and keep changing it to create different tables each time I run the package.


回答1:


Yes, This is possible. Here is how I would do it:

  • Build another variable for your SqlStatement from your TableName variable. Depending on your version of SSIS this can bedone in two ways:

    In SSIS 2008 you need to use Script Task to do the string concatenations.

    In SSIS 2012 you can leverage the Expression Task.

  • You can use the Exressions tab in Execute SQL Task and add an expression for SqlStatementSource and point it to your Variable. the variable should be the complete statement.



来源:https://stackoverflow.com/questions/18688912/dynamically-decide-the-table-name-in-execute-sql-task-for-create-table-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!