I have a package, that for testing purposes I've isolated out the piece that is failing. It's a foreach loop, that reads in the table name. Inside the loop, I have a SQL task that counts the rows in the table, and outputs the count to a variable (User::intRowCount).
Then it moves to another SQL task, which if that RowCount is greater than zero, it performs another SQL statement on a different database. So, the reason they are two SQL tasks, is that they have two different connection managers. I've set break points on
my loop and watches for my variables (table name and row count), so I can see what the values of my variables are. The value of my User::intRowCount is zero at the time of OnPreExecute it appears according to my watch. Yet, when the SQL task executes, it fails
as it is performing the command it should ONLY be doing if the rowcount > 0. I'm stumped. The variable type for the package is int16 (doesn't seem to be just an int), and I declare the variable as int within my SQL statements. (see below). Why is this executing
and not kicking out to the next iteration of the loop?
The "Execute SQL Task" does this:
DECLARE @TableName varchar(max)
DECLARE @CountCommand nvarchar(max)
DECLARE @RowCount int
SET @TableName = ?
SET @CountCommand = 'SELECT @RowCount = COUNT(*) FROM [' + @TableName + ']'
EXEC sp_executesql
@query=@CountCommand,
@params=N'@RowCount INT OUTPUT',
@RowCount=@RowCount OUTPUT
GO
...which should output the rowcount for the table, to the variable @RowCount. The task is set to a "None" result set. I have my User::intRowCount set to an initial value of zero.
The "Import" SQL task that follows, is also set to ResultSet of "None", has in the parameter mappings the table name (string) and the row count (int16) variables.
Which then executes the following SQL statements with a few tweaks in this display to remove irrelevant private info:
DECLARE @TableName varchar(max)
DECLARE @CopyCommand nvarchar(max)
DECLARE @RowCount int
SET @TableName = ?
SET @RowCount = ?
IF (@RowCount) > 0
SET @CopyCommand = 'copy ' + @TableName + ' from directory-' + @TableName + '.csv'' NULL AS ''\0'''
EXEC (@CopyCommand)
GO
The command works just fine, that's not the issue. The issue is WHY is it executing the command at all?
I really need help on this one folks.
mpleaf