在 CREATE TABLE 语句的执行 SQL 任务中动态决定表名 [英] Dynamically decide the table name in execute SQL task for CREATE TABLE statement

查看:50
本文介绍了在 CREATE TABLE 语句的执行 SQL 任务中动态决定表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否可以在 SSIS 执行 SQL 任务中完成.我想检查一个表是否存在,如果它不存在,则创建该表.我想将表名保留在一个变量中,以便我可以针对不同的数据库执行任务并能够更改表名.

举个例子,我有这个代码:

如果不存在(select * from sysobjects where name='Table1' and xtype='U')创建表 Table1(Col11 varchar(10) 主键,Col12 varchar(10));

为了灵活性,我想将值 Table1 存储在一个变量中,并在每次运行包时不断更改它以创建不同的表.

解决方案

是的,这是可能的.这是我的做法:

  • 从您的 TableName 为您的 SqlStatement 构建另一个变量多变的.根据您的 SSIS 版本,这可以分为两个部分方法:

    在 SSIS 2008 中,您需要使用 脚本任务进行字符串连接.

    在 SSIS 2012 中,您可以利用表达任务.p>

  • 您可以使用 Execute SQL Task 中的 Exresions 选项卡并添加一个SqlStatementSource 的表达式并将其指向您的变量.变量应该是完整的语句.

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.

解决方案

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.

这篇关于在 CREATE TABLE 语句的执行 SQL 任务中动态决定表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆