Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSIS: Variable from SQL to Data Flow Task

Pretty new to BI and SQL in general, but a few months ago I didn't even know what a model is and now here I am...trying to build a package that runs daily.

Currently running this is Excel via PowerQuery but because the data is so much, I have to manually change the query every month. Decided to move it into SSIS.

Required outcome: Pull the last date in my Database and use it as a variable in the model (as I have millions of rows, I only want to load lines with dates greater than what I have in my table already).

Here is my Execute SQL Task: enter image description here enter image description here

I set up a variable for the SQL query enter image description here

and trying to use it in my OLE DB query like this enter image description here

Execute SQL Task: results, are fine - returns date as "dd/mm/yyyy hh24:mi:ss"

SELECT MAX (CONVACCT_CREATE_DATE) AS Expr1 FROM GOMSDailySales

Variable for OLE DB SQL Query:

"SELECT fin_booking_code, FIN_DEPT_CODE, FIN_ACCT_NO, FIN_PROD_CODE, FIN_PROG_CODE, FIN_OPEN_CODE, DEBIT_AMT, CREDIT_AMT, CURRENCY_CODE, PART_NO, FIN_DOC_NO, CREATE_DATE
FROM cuown.converted_accounts
WHERE (CREATE_DATE > TO_DATE(@[User::GetMaxDate],'yyyy/mm/dd hh24:mi:ss'))
AND (FIN_ACCT_NO LIKE '1%')"

Currently getting missing expression error, if I add " ' " to my @[User::GetMaxDate], I get a year must be between 0 and xxxx error.

What am I doing wrong / is there a cleaner way to get this done?

like image 562
donDT Avatar asked Jan 18 '20 21:01

donDT


People also ask

How do you use a variable inside SQL in SSIS Data Flow tasks?

Select the SQL command option for data access mode, and then type a parameterized query in the SQL command text pane. Click Parameters. In the Set Query Parameters dialog box, map each parameter in the Parameters list to a variable in the Variables list, or create a new variable by clicking <New variable>. Click OK.

How do you pass a variable in an execute task in SSIS?

Execute SQL Task in SSIS allows user to execute parameterized SQL statement and create mapping between these parameters and the SSIS variables. To add a parameter into a SQL statement you must use a parameter marker which differs based on the connection type. Select * from table where ID > ?

How do I pass variables in SSIS package?

Open the package in SQL Server Data Tools, and then click the Parameters tab in the SSIS Designer. Click the Add Parameter button on the toolbar. Enter values for the Name, Data Type, Value, Sensitive, and Required properties in the list itself or in the Properties window.


1 Answers

In the OLEDB source use the following, change the data access mode to SQL command, and use the following command:

SELECT fin_booking_code, FIN_DEPT_CODE, FIN_ACCT_NO, FIN_PROD_CODE, FIN_PROG_CODE, FIN_OPEN_CODE, DEBIT_AMT, CREDIT_AMT, CURRENCY_CODE, PART_NO, FIN_DOC_NO, CREATE_DATE
FROM cuown.converted_accounts
WHERE (CREATE_DATE > TO_DATE(?,'yyyy/mm/dd hh24:mi:ss'))
AND (FIN_ACCT_NO LIKE '1%')

And click on the parameters button and map @[User::GetMaxDate] to the first parameter.

For more information, check the following answer: Parameterized OLEDB source query

Alternative method

If parameters are not supported in the OLE DB provider you are using, create a variable of type string and evaluate this variable as the following expression:

"SELECT fin_booking_code, FIN_DEPT_CODE, FIN_ACCT_NO, FIN_PROD_CODE, FIN_PROG_CODE, FIN_OPEN_CODE, DEBIT_AMT, CREDIT_AMT, CURRENCY_CODE, PART_NO, FIN_DOC_NO, CREATE_DATE
FROM cuown.converted_accounts
WHERE CREATE_DATE > TO_DATE('" + (DT_WSTR, 50)@[User::GetMaxDate] +
"' ,'yyyy/mm/dd hh24:mi:ss') AND FIN_ACCT_NO LIKE '1%'"

Then from the OLE DB source, change the data access mode the SQL Command from variable and select the string variable you created.

like image 80
Hadi Avatar answered Nov 08 '22 04:11

Hadi