views:

300

answers:

3

Using SQL Server 2005, but still using DTS. I need to add a step to check if a table is empty, and somehow fail the step if it is. It's easy checking if the table is empty:

Select count(*) from source_table

But returning 0 is a success, too. If it is 0, I want a failure (so that I can fork to a different option, email us, skip some steps).

A: 

what about returning -1 ?

Select 
   case when count(*)>=0 then count(*) else -1 end 
from 
   source_table

If you really want to raise an error, you can use RAISERROR to do just that :

Declare @RowCount as bigint
set @RowCount=count(*) from source_table
if RowCount =0
RAISERROR ('error message', 50000, 1)  with log
Brann
How do you use RAISEERROR in a DTS Execute SQL Task?
thursdaysgeek
@thursdaysgeek : I've updated my answer with an example
Brann
A potentially better way to check for an empty table:if not exists (select top 1 0 from source_table) raiserror(...)
Crappy Coding Guy
A: 

I didn't use DTS. It was resolved in SSIS, although, looking back, I could have probably done something similar in DTS.

Step 1: A data flow task that selects count and saves that count to a variable. The select count took a bit of work:

select cast(count(*) as integer) as Row_Count from MyTable

and then the output of the data flow task was a script component that was a destination and had an input column as that ROW_COUNT, and my ReadWriteVariables as TableCount (the variable that was used as input in step 2)

Step 2: A script task that evaluates that count and fails if the count was 0, succeeds otherwise. Forking from this is a success route and a failure route.

thursdaysgeek
A: 

Use the tools that are available in SSIS and drop in a conditional split. Then you can test the count variable. If 0 then do your email and skip some steps. Otherwise continue processing.

CTKeane