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;
Related
Ive got another task elsewhere in a package that I build a sql command from a script task, but I now have a simpler sql task that I want to parameterize the number of records I fetch, and drive this parameter via a package variable.
Now:
SELECT TOP 10 col1,col2,col3 from TABLE-A
What I need to use
eg. SELECT TOP ? col1,col2,col3 from TABLE-A
Where ? corresponds to a package variable of type int.
Can I do this with just the SQL task, and not have to derive the statement in something like a script task first?
[ update ]
Solution is what I was suspecting, I just added another script task before the sql task, and generate the statement there. Then change the sql task to use the variable that contains the statement.
Use the variable to build a dynamic sql string in a separate variable, and then execute the dynamic sql string variable.
Parameters can only be used in the WHERE clause.
As Tab mentioned, you will need to create a variable expression to construct the SQL statement. Then, in the Execute SQL Task, you will tell it to use a SQL String (see bullet point 4 in the section "Map query parameters to variables").
https://learn.microsoft.com/en-us/sql/integration-services/control-flow/execute-sql-task?view=sql-server-2017#parameters-in-the-execute-sql-task
declare #intVal as int = 5
select * from
(
select ROW_NUMBER() over(order by Id) as RowNo, * from <tableName>
)a
where a.RowNo <= #intVal
here #intVal will be parameter should be mapped in SQL task
I want to check the values of several variables before executing a sql task. The sql task is the 1st task within a container. Since there is no direct task before this one, I have no constraint to put an expression on. What could be used in this case?
If version of SSIS is 2012+, then Expression Task is to be added as a predecessor of SQL Task.
In other cases you can just add a dummy SQL task with something like SELECT 0 and setup then some expression on a constraint between that dummy and SQL tasks.
Alternative approach: you pass variables into SQL task and perform validation there:
DECLARE #_param1 VARCHAR(50) = ?
DECLARE #_param2 VARCHAR(50) = ?
IF #_param1 = 0 AND #_param2 = 0 -- check variables values
RETURN
ELSE
BEGIN
-- your SQL code
END
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
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...
I am a complete beginner in terms of SSIS packages.
I really want to execute a stored procedure that takes in parameters with different values at each iteration of the foreach loops. So I'm wondering if anyone can give me an example (VERY VERY VERY basic example) on how I can use variables as values inside an Execute SQL Task like this:
UPDATE tbName SET c1 = Var1, C2 = Var2 etc...
OR
#bDate = VarDate1
#eDate = VarDate2
where Var2, VarDate1, VarDate2 are variables in BIDS
First you need to create the stored procedure on the SQL Server end. This is done with a statement like this. After this has been ran a new stored procedure object called "yourProcedure" will exist in the database.
CREATE PROCEDURE yourProcedure
#pKeyVar int, /* declare variables to be passed here */
#pFirstVar varchar(40),
#pSecondVar int,
#pThirdVar decimal(18,2)
AS
BEGIN
Update yourTable Set /* place what will be done here */
Col1 = #pFirstVar,
Col2 = #pSecondVar,
Col3 = #pThirdVar
WHERE KeyColumn = #pKeyVar
END
GO
Once the stored procedure has been created you can call it like this:
exec dbo.yourProcedure 12345, 'string value', 2, 2.05
There are a couple ways to call it from SSIS, but the most common is the Execute SQL Task. You can map the parameters that will be passed to the variables that hold the content and put the task inside your looping logic.
Here is a decent walkthrough of the Execute SQL Task.
Pay close attention to the section on mapping parameters to variables etc. The version of SSIS is 2005 but the concepts are all the same.
Update after comment.
In order to loop through a recordset and obtain values to pass back to the proc you can follow the information as provided in this article.