temp table ssis using only Execute SQL Task - ssis

I add my simple process
as you see at first Execute SQL Task called "trae productos #prod"
this create and fill the temp table #prod
in the second Execute SQL Task called "select temp" I use #prod for any thing
for example select, update, delete etc..
but this does not work I have change the property "RetainSameConection" to True and
all object have DelayValidation=True but this does not work, but this does work if I use a ##temptable and this does not work if I use a #Temptable, but I need to use a #Temptable
how can I get this work in other Execute SQL Task with 1 #.
this second image is inside a loop foreach before this loop i want to create my temptable

Related

SQL Query execution in SSIS

Finding issue in achieving below problem in SSIS.
I have a variable in SSIS #Select which is initialized with a SQL task in SSIS as below.
#Select='Select column1, column2 from tableName', like a dynamic select query, now I want to Execute #Select variable(which should execute select query inside it) to return the full result set in a separate SQL task I have tried it like below but not succeeded.
Declare #Query Varchar(2000)
SET #Query=? // here ? will store the select query in #Select variable
EXEC (#Query) // executing to return result set??
Can anyone help me to achieve this??
If you want to execute a dynamic select query in SSIS then use an Execute SQL Task and edit the task.
Set the ResultSet option to Full Result Set if you are expecting multiple rows and Single Row for only one row. Add your DB connection name to the connection property and ensure that it is in the list of connection managers and configured correctly. You can set the SQLSourceType to Variable and specify the variable you want to use below that. Although using the Direct Input option is just as good where you specify your SQL Statement. Now set the variable you want the ResultSet to write to in the Result Set tab, which is in the left column of the task editor. you can even specify the parameter variable you would like to use in the Parameter Mapping tab.
You can click on Build Query to see if your query works. Hope this helps and let me know if I missed anything :)

SSIS: ADO.NET Informix source doesn't work with temp table

I'm trying to create SSIS package to perform a relativwely simple ETL.
My connection manager is DB2 .Net provider (.Net Providers/IBM DB2 .NET Data Priovider).
My data source is ADO NET Source. It's working in SQL command mode.
Following command works:
select first 10 *
from part
But this doesn't work:
select first 10 *
from part
into temp temp2;
select *
from temp2;
drop temp table temp2;
Actually it behaves pretty odd.
After first execution (press button 'Preview...') I usually get this error:
Pipeline component has returned HRESULT error code 0xC02020FE from a method call. (Microsoft.SqlServer.DTSPipelineWrap)
After second execution (press button 'Preview...' again) I get the data set!
However, after third execution (and consecutive executions) the error tells that...
ERROR [IX000] [IBM][IDS/UNIX64] Temp table (temp3) already exists in session. (IBM.Data.DB2)
Googling leads me nowhere :(
Is there any logic behind this, and how to fix it?
UPDATE:
After encapsulating the query in the STORED PROCEDURE and calling it using EXECUTE PROCEDURE like this...
CREATE PROCEDURE MY_DATA()
RETURNING CHAR(256), CHAR(256);
DEFINE l_fld1 CHAR(256);
DEFINE l_fld2 CHAR(256);
SELECT first 10 fld1, fld2
FROM part
INTO TEMP temp2;
FOREACH
SELECT fld1, fld2
INTO l_fld1, l_fld2
FROM temp2
RETURN l_fld1, l_fld2
WITH RESUME;
END FOREACH
DROP TEMP TABLE temp2;
END PROCEDURE
...I have a few new problems:
after clicking "Preview..." first I get result and on ther second click I get ERROR [IX000] [IBM][IDS/UNIX64] Temp table (otkalcec.temp2) already exists in session.
if I wait a little bit and click "Preview..." again, it works again; obviously the session is dropped in the meantime (some idle timeout?)
nevertheless, query always returns with empty fieldnames and that may be the cause I don't have any columns mapped when i choose "Columns" on the left :(
UPDATE 2:
There was a problem related to the SSIS consuming Informix driver parameter which I didn't know it existed: "Connection lifetime". This solves first two problems.
Now the preview resultset works fine. The only problem I have is missing column information in Columns. Everything seems to be working except THAT, and I can't proceed without freaking column information! >:(

SSIS Using Foreach Container in certain way

I am trying to use a Foreach Loop Container in SSIS to loop through a small set of rows and set the column 'id' to a variable.
I have the following variables:
#startNumber = 5
#maxNumber = 10
Here's how my loop would look in traditional syntax:
for (int i = #startNumber; i > #maxNumber ; i++)
This is the TSQL code I want to run to update my rows:
UPDATE myTable SET id = i
Would I use a Foreach ADO Enumeration?
What would be my source variable?
How would I apply the logic above?
Would I include an Execute TSQL Statment for my update?
Any help is greatly appreciated.
I created a simple control flow to show you what I think you're after
I start with an Execute SQL Task to generate a basic table. You would probably have logic here instead to determine the starting number and/or the terminal value
if not exists
(
SELECT * FROM sys.tables AS T WHERE T.name = 'FLC'
)
BEGIN
CREATE TABLE dbo.FLC
(
currentID int
);
END
3 variables, scoped to package level
I configure the For Loop (not the foreach as your question has tagged) much as one would a classic for loop. Here I'm assigning a value to my SSIS Variable #currentNumber
Within my Execute SQL Task (inside the for loop) I am using #currentNumber as a parameter to my task.
INSERT INTO
dbo.FLC
(
currentID
)
-- OLE DB & ODBC connections use ? for ordinal parameter
-- ADO.NET uses named parameters like #foo
SELECT ? AS currentID;

SSIS Execute SQL task with parameter

I need to execute sql task based on parameter.
Lets say if my #parameter = 1 then execute this sql if #parameter = 2 then execute this sql. I think of a work around but is there anything straight forward such as Len(?) or Len(#parameter1) ..
Bottom line: I need to execute sql query based on what's passed to parameter.
Let me know if that's possible.
If you want an Execute SQL Task to run a different stored procedure based on a variable, then there are a few options:
You could create a stored procedure that takes a parameter. The stored procedure would use IF ELSE code to execute the code as described in a comment by Lamak. This is a less than ideal solution if you want to execute different stored procedures. This could work if you only have a very small number of queries or stored procedures to execute.
You could write a variable that calculates the name of the stored procedure based on an expression. This could work well if you only have a few stored procedures to execute, but it does not scale for a large number of stored procedures. It also is hard to understand from a coding perspective, particularly if the expressions are complex.
You could write a query or stored procedure that generates a separate stored procedure call command. You could run an Execute SQL Task the loads a result set. The result set would map to a variable of Object data type. You could then iterate through the variable in a For Each Container to assign values to variables. Easier to manage than 100 expressions if you have a lot of code to vary.
Based on your comment to me it sounds like you want to try option 2. The following are detailed steps for option 2:
In the Variables window at the package-level scope create a variable called SqlCommand of data type String.
Set the EvaluateAsExpression property for the SqlCommand variable to True.
Click on the expression builder link.
The following is a sample IF THEN ELSE expression using the Conditional operator.
1 == 0 ? "SELECT SomeField = GETDATE();" : "SELECT SomeField = GETDATE() - 2;"
If 1 equals 0, then the first command will be returned. If 1 does not equal 0, then the second command will be returned. In this case, since 1 does not equal 0, the second command is returned. You can change the 1 == 0 section to be the condition you actually want to evaluate.
Add an Execute SQL Task to the control flow.
Open the Execute SQL Task Editor.
Set Connection to your desired database connection manager.
Set SQLSourceType = Variable.
Set SourceVariable to User::SqlCommand.
Close the editor and test the package.
user1810575 has asked this question again in ssis-execute-sql-task-based-on-parameter, see my answer (which is copied here as well).
You cannot use Execute SQL Task to run Transact-SQL statements.
For setting conditional SQL Statement based on what you are trying to achieve.
In Execute SQL Task editor
In general tab, leave the SQLStatement blank.
In parameter mapping tab, add parameter and map User::Parameter variable to Parameter Name 0.
In Expression tab, set the SQLStatementSource to
(DT_NUMERIC, 18, 0) #[User::Parameter]==1 ? ...query 1... : ...query
2...

SSIS Execute SQL task based on parameter

Can i do something like below, let me know
IF #parameter=1 BEGIN ...query... END IF #parameter=2
Need the correct syntax if it is possible.
It's OLE DB connection.
Not a Stored Proc. just a sql query
DECLARE #param AS INT = ?;
IF #param = 1
BEGIN
SELECT 1 AS Y;
END
ELSE IF #param = 2
BEGIN
SELECT 2 AS Y;
END
There are two question marks in your query and probably you were passing only one variable. I have seen code where developers pass the same value twice (or multiple) times. This is inefficient. A better way is to receive the passed parameters in SSIS variables. Advantages:
1. You need to pass one value only once.
2. More importantly, if you change the order in which the passed parameters are used in the sql, you do not need to change their order on the user-interface of Execute SQL Task Editor//Parameters. This is what Andy Leonard has suggested later in his response.
You can. Assuming you are referring to an Execute SQL Task, the parameters in an Execute SQL Task using an OLE DB connection utilize question marks (?) as parameter placeholders. You map the placeholders to SSIS variables on the Parameter Mapping page of the Execute SQL Task. In the SQLStatement property you would enter:
If (?=1)
begin
... {some T-SQL here} ...
end
If (?=2)
begin
... {some T-SQL here} ...
end
That's one way to accomplish what I think you are asking.
Another way is to create an Execute SQL Task to read the value of #parameter from the database into an SSIS variable. Then you can build two Execute SQL Tasks - one with each option for T-SQL as the SQLStatement property - and use expressions on precedent constraints to determine which Execute SQL Task to execute.
Hope this helps,
:{>
You cannot use Execute SQL Task to run Transact-SQL statements.
For setting conditional SQL Statement based on what you are trying to achieve.
In Execute SQL Task editor
In general tab, leave the SQLStatement blank.
In parameter mapping tab, add parameter and map User::Parameter variable to Parameter Name 0.
In Expression tab, set the SQLStatementSource to
(DT_NUMERIC, 18, 0) #[User::Parameter]==1 ? ...query 1... : ...query 2...