简体   繁体   中英

SQL Variables Change in SSIS

I'm new to SSIS so forgive me if this question is trivial or has already been answered. So I have a SQL query that begins as follows:

declare @start datetime, @end datetime, @startMonth datetime, @endMonth datetime, @maxHoursToRespond int
    ----Set These------------------------------------------------
    set @end='6/27/2014'
    set @maxHoursToRespond=24                       
    -------------------------------------------------------------
    set @start=dateadd(dd, -90, @end) -- set duplication period
    set @startMonth=dateadd(dd, -2, @end)-- set to start date of output you want
    set @endMonth=dateadd(dd, -1, @end) -- set to day of end date of output you want

When I put this in my OLE DB Source Editor with SQL command as my data access mode, all the variables are replaced with question marks. It looks like :

DECLARE ? datetime, ? datetime, ? datetime, ? datetime, ? int
/*--Set These------------------------------------------------*/ SET ? = ?
SET          ? = 24
/*-----------------------------------------------------------*/ SET ? = dateadd(dd, -     90, ?)
SET          ? = dateadd(dd, - 2, ?)
SET          ? = dateadd(dd, - 1, ?)

In the query builder.I'd like to know why this is happening.I'd also like to know how I can allow the query to be successfully built (currently I get a syntax error of "The Declare SQL construct or statement is not supported."). Do I have to create these variables (like @start) in SSIS itself?

You can:

  1. Encapsulate your SQL in a stored procedure that contains all of the declarations internally (assuming the variable values are static), then call the stored procedure in the execute SQL task with EXEC. EXEC My_Stored_Procedure
  2. Write a stored procedure that accepts the variables as inputs, map them to variables in SSIS, then execute the stored procedure like this Exec My_Stored_Proc ?,?,?,? with the user variables mapped to the corresponding stored procedure variables.
  3. Leave the query as is, but remove the DECLARE and the SETs, and map the SSIS variables to the query. This most likely will not work, because SSIS will not know which ? corresponds with which variable (it will try to map them in the order they appear.

Number 2 is the generally accepted method, unless you store your variable values in a table or something that the SP in number can access, in which case number 1 may be cleaner.

I have pasted straight to SQL command text yours script in OLE DB Source Editor and nothing changed in it. By pressing Parse Quesry.. I checked that SQL is correct. When I tried to use Build Query.. , it said, that DECLARE is not supported. So don't use builder :)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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