SSIS can't use variables (Flat File > OLE DB Command), Must declare the scalar variable "#" - sql-server-2008

Apologies, I'm an SSIS noob and I'm struggling with trying to import a flat file and do an OLE DB command WHILE including a variable in the SqlCommand.
You can see my query and the attempt to include an already defined variable. The error is displayed below:
Must declare the scalar variable "#" I understand what the error is saying, but after hours of searching, I cannot figure out how to use variable in the OLE DB Command.
Thanks in advance for any assistance.

Try putting the ssis variable [user::ClientID] into a derived column and add it to the data flow. You should be able to use a ? and assign that value to your query just like any other value in your data flow.

Related

Can you pass GETDATE() to a SQL Job that runs SSIS Packages (Set Values)

I haven't been able to find a straight forward answer to this. What I'm trying to accomplish is to execute a SSIS package that requires variables to be set, so I need to pass these variables to SSIS through the SQL Job. I know this is possible through the "Set Values" tab, but I need to pass the current date, so hard-coding a string will not work.
Is it possible to pass GETDATE() or other expressions to the Value field within Set Values? In other words, does the "Value" field get evaluated at runtime?
If this is not possible, what would be a good solution to pass the current day from the SQL job to the corresponding parameter in the SSIS package?
Unfortunately, no. It is not possible assign a method such as GETDATE() to the value property of a datetime variable through the Set Values screen of the SQL Agent Job. The value fields there must be set to discrete values.
I can think of two ways around this.
The first - and easiest option - is editing the package. Open the package with BIDS/SSDT, find that variable, and set that "Evaluate As Expression" property of that variable to True. Then in the properties window of that Variable, click the Expression ellipsis... and set Expression equal to GETDATE(). Save package. When the package runs, that variable will evaluate to GETDATE().
The second method takes a bit more sweat, and has risks, but allows you to dynamically set SSIS package variables. Instead of using SQL Server Job to call a package. Scheduled job which calls an SP. This SP will then execute the SSIS package using xp_cmdShell and the DTEXEC.exe command line tool.
Here is an example. The DTEXEC.EXE command line tool has a /SET argument that basically does the same thing as the Set Values dialog box. But this way its dynamic. Inside the SP you can
DECLARE #MyDate datetime
SET #MyDate = GETDATE()
You just need to get your #MyDate value over to the /SET argument of DTEXEC.
Also, instead of GETDATE() you could also have a parameter that lets you build this date variable on the command line from a combination of substrings of the %DATE% and %TIME% system environmental variables.
set MyDate=%DATE:~10,4%-%DATE:~7,2%-%DATE:~4,2% %TIME:~0,8%
dtexec /FILE "\"D:\SSIS\Package1.dtsx\"" /SET "\"\Package.Variables[VarDateTime].Value\"";"\"%MyDate%\""
An example of that is over here.
Obviously, option one is easier and less risky. Good luck!
When a job kicks off an SSIS package, the parameters it passes have to be hard-coded in the job.
If you need to pass more context-sensitive values to the package, the best way I've found to do it is to populate a meta-data table in your database, and have the first step in the job be to select the latest row from that table and populate variables from it.

SSIS Package Based on a avariable

Hi I've an SSIS Package which Does tasks like a)Truncate Last 7 days data from a table if any and then re-load it and all theses are placed in a sequence container and it run's fine.Now I'm planning to remove the hard coded value of 7 and introduce a variable NoOfDays which I can provide at run time.Can this be achieved?
I added a variable and tried to map it to the parameter of the ExecuteSQL Task...It gave the following error:
I even want the value to be available to the next step Data Flow Task
[Execute SQL Task] Error: Executing the query "delete from USER_CONTENT where CONVERT..." failed with the following error: "The variable name '#NoOfDays' has already been declared. Variable names must be unique within a query batch or stored procedure.
Statement(s) could not be prepared.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
The query I'm using is
delete from USER_CONTENT
where CONVERT(date,ISSUE_DATE)>=CONVERT(DATE,GETDATE()-7)
and it is against an OLE-DB connection.
I do not have enough points to post a comment so I will have to ask you this way: are you using query parameters with a "?", if not, then in order to use a package variable with a stored procedure, you will need to write your statement to use them, also, don't name your parameters, use the default 0,1,2 etc.. that the package assigns.

SQL Task - Set Package variable

I have declared a variable at the package level compdate and am testing data flow to the variable by droping an Execute SQL Task in the Control Flow of the package.
In the task,
SQL Statement:
select ? = (getdate() - 1)
Parameter Mappings:
Variable Name: User::compdate
Direction: Output
Data Type: DATE
Parameter Name: 0
Parameter Size: -1.
Why am i getting error:
[Execute SQL Task] Error: Executing the query "declare #compdate date
set #compdate = (getdate() ..." failed with the following error: "Syntax error or access violation". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I do not see why you need to execute an SQL statement to get the previous day as this can be done in various other ways.
To answer your question though, since you are trying to store the result of the SQL query from your Execute SQL Task you have to change the SQL statement that you have provided.
Your new query:
SELECT (GETDATE() - 1) AS DateVar
Where DateVar will be the single parameter that is returned which you need to map to your variable.
You need to delete your Parameter Mappings as they are not needed. Open up the Result Set tab and Add a new result. Set the Result Name to be DateVar and set the Variable Name to be your variable User::compdate
You then need to set up your Execute SQL Task to return a Single Row result set in the General tab, mapped to your variable. Select Single row for the ResultSet option.
Working with result sets is explained in great details here. Scroll down to the 'Working with a Single-Row Result Set' section, it has a great example which you can follow.
If you want to use without using the result set. try with following steps.
Create the stored procedure in your respective database. Following
code is an example.
CREATE proc GetYesterDay(#yesterday datetime output)
as
Select #yesterday=getdate()-1
Create the ADO.NET connection to run the stored procedure. In which, you can mention the direction of the input and output of the parameters.
Create the execute task and configure it as following screenshot.
Click on Parameter Mapping and configure as following screenshot.
Now SSISCompletedDate variable will be filled with respective data.
Hope this helps!

How to create a dynamic IN query in SSIS 2008?

I have a variable #csv which hold a comma separated value such as:
-a
-a,b
-a,b,c
I need to pass it in a query in my OLE DB source in a data flow to create a query such as:
SELECT COUNT(1) FROM table WHERE col1 IN #csv
So if #csv="a,b" then internally it should resolve into
SELECT COUNT(1) FROM table WHERE col1 IN 'a','b'
How can this be best achieved in SSIS 2008? Can I avoid the script component to create a dynamic query and storing it in a variable?
How can this be best achieved in SSIS
2008? Can I avoid the script component
to create a dynamic query and storing
it in a variable?
The easiest/best way would still be with a script component.
Otherwise you could:
use the csv as data source and select your result
use the and "add column" tool to add the rest of your SQL query around the result
store the result into a variable
Then use a the OLE DB datasource with "query from variable"
You can create a variable to store the query and compose its value using an expression, like:
List of Variables:
Option 1: In case of using OLE DB, select SQL Command from variable and bind the variable #sqlQuery:
Option 2: In case of using ADO.NET, Go to properties of Data Flow Task and expand Expressions and bind the ADO.NET Source > SqlCommand with the variable #sqlQuery + Make sure that ADO.NET Source > Data access mode is a SQL Command:
Option 3: In case of using Execute SQL Task, expand Expressions and bind the SqlStatementSource with the variable #sqlQuery + Make sure that SQL Source Type is a Direct Input:

SSIS 2008 Execute SQL output parameter mapping datetime2 problem

I'm trying to use an Execute SQL Task in SSIS 2008 to map a store procedure output parameter to a package variable.
The package variable is SSIS type DateTime and the store procedure parameter is SQL type DATETIME.
The SQL Statement is EXEC GetCurrentDate #CurrentDate=? and in the parameter mapping screen, the parameter is mapped to the package variable with direction Output and Data Type DBTIMESTAMP specified.
When I run the package I get the following error:
[Execute SQL Task] Error: Executing
the query "EXEC GetCurrentDate
#CurrentDate=? " failed with the
following error: "The type of the
value being assigned to variable
"User::CurrentDate" differs from the
current variable type. Variables may
not change type during execution.
Variable types are strict, except for
variables of type Object. ". Possible
failure reasons: Problems with the
query, "ResultSet" property not set
correctly, parameters not set
correctly, or connection not
established correctly.
If I run a trace on the query being run I see the type is being assumed as datetime2:
declare #p3 datetime2(7)
set #p3=NULL
exec sp_executesql N'EXEC GetCurrentDate #CurrentDate=#P1 ',N'#P1 datetime2(7) OUTPUT',#p3 output
select #p3
Does anyone know why it is assuming the type is datetime2?
Thanks
Found the answer on a Micorsoft Connect bug report:
We are closing this case as this is expected behaviour and is a result of the new sql datetime type change. You are using a native oledb connection manager for sql task, in the process of COM interop, we use VARIANT to hold the value and the only way to prevent data loss is to store the value as BSTR variant. If you change User::dateParam to String type it will work, or you can switch to use managed connection manager to bypass the COM interop.
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=307835
Try specifying the inout/output parameters as DATE rather than DBTIMESTAMP in the SSIS task.
This certainly works in SSIS 2005 packages I've worked on.
It's also worth taking a look at this link, which covers this as well as a couple of other issues with SSIS and dates.