简体   繁体   English

SSIS传递变量以执行sql任务(创建表参数)

[英]SSIS passing a variable to execute sql task (create table parameter)

I have a package that contains two execute sql tasks followed by numerous DFTs. 我有一个包含两个执行sql任务以及大量DFT的程序包。 The first has the statement: 第一个有以下语句:

select CAST(floor(rand()*1000)+1 AS INT) AS SeqVar

And has the ResultSet Single row - this works perfect. 并具有ResultSet单行-完美的作品。 It gives me a random number between 1 and 1000 and passes that value on to a variable I have called SeqVar. 它给我一个1到1000之间的随机数,并将该值传递给我称为SeqVar的变量。 (I have also verified that this works) (我也已验证这可行)

The problem I am having is in my second execute SQL task where I try and use the SeqVar variable outputted from the first Execute SQL teask as a parameter in the following statement: 我遇到的问题是在我的第二个执行SQL任务中,在其中尝试并使用从第一个执行SQL脚本输出的SeqVar变量作为以下语句中的参数:

IF OBJECT_ID('tempdb.dbo.[##temp1]') IS NOT NULL
DROP TABLE [##temp1]

CREATE TABLE [##temp1] (
[RecID] int IDENTITY(?,1),
[Name] VARCHAR(30),
[ABA] VARCHAR(10),
[Account] VARCHAR(20),  
[Type] VARCHAR(1),
[Date] date,
[Amount] money   
);

Under parameter mapping I have the SeqVar variable name, Direction is Input, Data Type numeric, Parameter name is 0, and Parameter size is 1000. 在参数映射下,我具有SeqVar变量名称,“方向”为“输入”,“数据类型”为数字,参数名称为0,参数大小为1000。

The value I get has to go where I have the "?" 我获得的价值必须去我拥有“?”的地方。 in the create tempdb statement. 在创建tempdb语句中。 I am trying to have my code start at a random number and increment by 1. 我正在尝试让我的代码从随机数开始并以1递增。

I know this would probably be easier with a Script task but that tool is broken on my maching (weird dts pipeline errors). 我知道使用脚本任务可能会更容易,但是在我的处理中该工具已损坏(奇怪的DTS管道错误)。 Thanks in advance and this is all in SSIS 2008. 预先感谢,这一切都在SSIS 2008中。

Using identity in this way seems like a strange solution just to start a sequence at some random value. 仅以某个随机值开始序列,以这种方式使用身份似乎是一个奇怪的解决方案。 Parameters formatted as ? 参数格式为? definitely don't work in that context in SQL. 绝对不能在这种情况下在SQL中工作。

However, another way to manage this, if the method is a given, is to set the entire code of your SQL task using an expression where you sub in the value as a simple string concatenation and then run the resulting string. 但是,管理此方法的另一种方法(如果已给定方法)是使用表达式设置SQL任务的整个代码,在该表达式中,您可以将值作为简单的字符串串联而包含在其中,然后运行结果字符串。

Better do create table as like below then use RESEED property to set it to your Random Variable Value' 最好像下面这样创建表,然后使用RESEED属性将其设置为您的“随机变量值”

IF OBJECT_ID('tempdb.dbo.[##temp1]') IS NOT NULL
DROP TABLE [##temp1]

    CREATE TABLE [##temp1] (
    [RecID] int IDENTITY(1,1),
    [Name] VARCHAR(30),
    [ABA] VARCHAR(10),
    [Account] VARCHAR(20),  
    [Type] VARCHAR(1),
    [Date] date,
    [Amount] money   
    );

    DECLARE @RANDOM_VALUE INT = 50
    DBCC CHECKIDENT('##TEMP1',RESEED,@RANDOM_VALUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM