June 1, 2010 at 4:30 pm
I want to create an Execute SQL Task containing the following code:
[font="Arial"]SELECT COUNT(*)
FROM dbo.vwFileListForInsert
UNION
SELECT COUNT(*)
FROM dbo.vwFileListForUpdate[/font]
The result should be a single integer value. How can I assign that result to a variable that has a package-wide scope? Does it need to be stored in a variable with an Object datatype, or can it be an integer? If it is an Object datatype, how can I do something like the following:
If varCount > 0 Then. . .
Can an Object datatype support that type of activity?
June 1, 2010 at 6:40 pm
The only way that this query is going to return a single integer value is if both queries return the same value... and that's because of the UNION (vs. UNION ALL) statement. Otherwise, you will get two numbers back.
What you want to run is:
;WITH CTE AS
(
SELECT Qty = COUNT(*)
FROM dbo.vwFileListForInsert
UNION ALL
SELECT COUNT(*)
FROM dbo.vwFileListForUpdate
)
SELECT Qty = SUM(Qty)
FROM CTE
If you don't understand what's going on here, look up "WITH" in BOL.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 1, 2010 at 6:51 pm
Thanks, that helps me to retrieve the proper number. However, I'm still confused on how to assign that result to a variable so that I can eventually add the condition If var > 0 then . . .
June 1, 2010 at 7:09 pm
Did you try clicking the "Help" button on the Execute SQL Task Dialog box after going to the Result Set tab?
It goes to something like this.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 2, 2010 at 2:40 am
imani_technology (6/1/2010)
Thanks, that helps me to retrieve the proper number. However, I'm still confused on how to assign that result to a variable so that I can eventually add the condition If var > 0 then . . .
Set the ResultSet property in the General tab to Single Row.
Then, go the the ResultSet tab and map the index 0 to your variable.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply