SSDT/SSIS Execute SQL Task not populating variable with query output - sql-server

My Execute SQL Task uses a simple query that should be returning a single value. I want to write that value to a variable so that I can use it in a data flow task later but the result never seems to get written to my variable.
The SQL statement is simple:
select max (rec_id) from [dbo].[RX_BILLING_TEST]
and I believe that I've set up the task correctly:
When I execute the task, it completes successfully but the variables window shows that the value of my variable didn't update. I set breakpoints on variable value changed and on post execute but that didn't help.
As much as I hate errors, this is a case where I would appreciate getting one to point me in some direction. Any ideas what might be wrong?

In a similar case there are many things you have to check:
Try adding an alias to the aggregate function
select max (rec_id) AS Max_Rec_Id from [dbo].[RX_BILLING_TEST]
Check that your package doesn't have more than one variable User::v_Recid with different scopes. or that User::v_Recid scope's is Execute SQL Task. (it must be the Package)
Test your SQL Command using Sql Server Management Studio
Try adding the database name to your Command
select max (rec_id) AS Max_Rec_Id from [MyDB].[dbo].[RX_BILLING_TEST]

Related

SSIS Logging detail from SP with Full Result Set

(I am not a vested enough member to include screen shots in my post - here is a link to a shared OneDrive folder which has article with images to explain better https://1drv.ms/f/s!Ai9bJ2dV87SLg9h5FP84If74hyUK4Q)
I am trying to log what particular stored procs have inserted, updated and deleted after they were executed via Execute SQL task within a SSIS package workflow. There was a custom logging method that a 3rd party implemented, but it worked by relating a System ParentContainer ID to a user Task ID which served as a parameter to a stored procedure which logged such information. 1) I don’t think this will work from an Execute SQL Task and 2) I want a level of detail that extends past what DML function occurred.
I have been successful in logging a “single row” by setting up a result set, using variables and via an adjacent Data Flow task using a derived column task to retrieve the variables and insert into a log table.
As an example of my current working method:
The Exec SQL Task setup
Detail of the data flow part that logs
I am now coming across stored procedures that perform multiple inserts, thus I have the need to log the additional detail - more than one row. I created variables in the proc to retrieves this multiple INSERT scenario and have a union select in the SP that yields the following result set.
I understand that I now need to use the Full Result Set setting now but for this application what method is used for to persist the result set to another step (for me a destination in order to log). Amongst research I understand how one may use a Foreach loop container but this appears to configurable to one variable which needs to be value type object. I currently have 4 variables here and unable to setup as such in the Collection section of the Foreach object.
I would appreciate any insight in achieving this or suggestion to another method altogether.
many thanks in advance!
Your INSERT_B_BUDGET SQL task generates more than 1 row, you want to use Foreach Loop to catch the full result set, correct?
create a variable, LoopObject, data type as Object.
edit INSERT_B_BUDGET task,
in General tab, change ResultSet to Full result set
in Result Set tab, Variable Name as LoopObject.
add Foreach loop container after INSERT_B_BUDGET task, move your Logging 1 task into the container.
in Collection tab, Enumerator, select Foreach ADO Enumerator, ADO object source variable as LoopObject, Enumeration mode, select Rows in the first table.
in Variable Mappings, add your existing 4 variables.
You have to set up a profiler trace to track what procs are being executed. You should set up filters on the trace by database and user and use the SQLProfilerTSQL_SPs template.

Call sproc with temp tables from SSIS in visual studio 2012

I am new to SSIS and would appreciate any help on my issue below -
I have a stored procedure which does not take any input and returns a temp table. This is used for data validation and will be run every day thus, I need to create an SSIS package for the same (the requirement is such). I created an Execute SQL task where I have the result set in a variable of type Object but I want to add a condition of checking rowcount >1 and if yes then write to an excel file.
Thanks,
Hiral
Answer depends on whether you are using SSIS 2012+ or 2008.
Assuming you are with SSIS 2012+:
Approach - declare variable type int for rowcount. Process received Object variable in Foreach loop, more details and screenshots are available in this article. Inside loop - increment variable. Then you can do a conditional execution based on rowcount variable value > 0.
Alternatives - valid for SSIS 2008 as well:
Object with result set is nothing more than a .NET dataset. Create a Script task which counts rows in the first table of the dataset, and stores this in a variable. Then use this variable in package as described above.
Instead of using Execute SQL Script task for SP run, use DataFlow with OLE DB Source and specify the stored procedure as the data source. Then you can count the resulting rows, store value in a variable etc.
Change property Retainsameconnection =true for connection manager
Doing so you can access temp table created in previous step .
You could use a global temp table instead if you want the creation done in a separate step. But this comes with its own issues like being accessible to everyone/every process.

Inserting Data into SQL Server from variables via SSIS

I have consumed simple web service for addition of two numbers and I am getting the result in xml format that I have stored in variable supposed named as my_data, now I have to insert that result in my sql table named as Data_result by writing query in oledb source editor, but unable to insert data using variable.I just want to store all the result calculated by my web service in sql table.
Since I am newbie to SSIS, any help is greatly appreciated
When I am trying to insert the data by the following query command:
"INSERT INTO Data_Result(Result) SELECT '"+#USER::my_data+"'"
It's giving error:
Error at Data Flow Task [OLE DB Source [55]]: No Column Transformation was return by SQL Command
--------Updated explanation of errors based on the solution given
Error for executing query
If I place the expression without the "" quotes then I get following error
If I place the query in double quotes then following error is shown
And if I remove User from User::Data Variable and place the query in double quotes then I get following screen
although the expression evaluates but after proceeding further on this evaluated expression when I am trying to search for the variable in expression column of Execute sql Task, then I am unable to locate the newly created variable, as shown below
------------Updated question according to other query----------
Here is the picture of my whole work flow
This is what I have did inside the for each loop container under collection tab
And this below setting I have done between Variable mapping tab,
And in below screen shot, I am using Execute SQL Task to enter my data obtained from web service task into database using an insert query, but unable to fire proper insert query,
And below is my XML file,
<?xml version="1.0" encoding="utf-16"?>
Reshma
1988-09-23T00:00:00
,
This name and birthdate I received from web service I want to insert that into database table
--------------Updated question for foreach loop container--------
Below is the foreach loop container I am using,
But still I am not getting node value into variable and also I am suppose to make use of object type variable or it can work with string type of variable.
I suggest you build this SQL statement in another variable, as explained here:
http://consultingblogs.emc.com/jamiethomson/archive/2005/12/09/2480.aspx
Then use that variable in your execute SQL statement
Normally you would try and parameterise the query but in this case (as in many others) it won't work.
Here are some more detailed instructions:
A. Generate the dynamic SQL
Create a new SSIS variable of type string
In properties, set it's 'Evaluate as Expression' to TRUE
In properties, click on 'Expression' and build an expression as follows:
"INSERT INTO Data_Result(Result) VALUES ('"+#[USER::my_data]+"')"
Press Evaluate and verify that the output is valid SQL (you may even wish to paste it into SSMS and test)
B. Get an Execute SQL task to run your SQL:
Drop a execute SQL task on your page. Set it's connection
With the execute SQL task highlighted, go to properties and click expressions
Pick the property 'SQLStatementSource'. In the expression pick your variable that you built in step A and press evaluate.
Press OK on everything
Now whatever change occurs in my_data will automatically be applied to your execute SQL task
Try that for starters and get back to me.

how to load a variable from "Execute Sql Task" in SSIS package

I have some variables (at package level, at dataflow level, doesn't matter) and I need to load them by running an Execute Sql Task.
I added an Execute Sql Task, I have wrote my sql statement in filed SqlStatement, I have changed the ForcedExecutionValueType to string.
Now I want that when the Execute Sql Task is executed the return value that that select returns, to be the exact value for my variable.
example:
Select employer_name from employers where id=1
Returs="John Doe"
value for #employer_name must be "John Doe"
Any hints?
Your more detailed discussion of the issue is a little difficult to follow, so I aplogize if my following steps cover items with you are already familiar.
Steps:
Under the Control Flow tab, drag over an Execute SQL Task and open it.
Select General on the left.
Define the Connection (based on a database source created in Connection Managers below).
Define the SQL Statement.
For Result Set, select Single Row.
Select Result Set on the left.
Select the Add button and notice that a new record was added above.
Under the Result name column header for the new record, change "NewResultname" to "0".
Under Variable Name column header for the new record, select either an existing User variable you've already created or scroll to the top and create a new one.
Select OK and you're done.
Running the Execute SQL Task by itself will now populate the variable. Make certain you've verified that the SQL will return only one value and no more. Otherwise, you will need to modify your SQL with "TOP 1" in it to be on the safe side. When multiple values are expected, then you to apply a variable defined with a Data Type of "Object" and use "Full Result set" instead of "Single Row" in the Execute SQL Task.
Hope this helps.

SSIS variables not containing correct values when running as job

In ssis(2005) I am using an execute SQL task to call a stored procedure. The stored procedure takes 3 params, all 3 are ssis variables. My package executes without problem in dev but fails when running as a job.
The log gives the following msg: Executing the query "Exec nx_sp_WriteFLHeader ?,?,?" failed with the following error: "Unspecified error". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Using Sql server profiler I can see the stored procedure is called with incorrect values, (1,null,null) in dev it would be somthing like 454,404,"sometext".
When running as a job I can see the ssis variables successfully being used in in other tasks, it is if the variables are out of scope by the time I am trying to use them. I did declare the variables at the highest level.
Thanks
Solved: In the execute SQL task, Parameter Mapping section, one of the parameter's Data Type's was defined as Numeric. After changing it to Long my job executes successfully. The ssis variable I am passing was declared as an int32.
Solved: In the execute SQL task, Parameter Mapping section, one of the parameter's Data Type's was defined as Numeric. After changing it to Long my job executes successfully. The ssis variable I am passing was declared as an int32.

Resources