Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 need to make a job exit if no data found

Author  Topic 

mayerl
Yak Posting Veteran

95 Posts

Posted - 2011-05-10 : 09:43:54
Good morning,

I have been asked not to send an empty file to our shipment database, a reasonable request. I was thinking I could make a step in the job that says look at today's data is there any?:


select case when COUNT(*) > 0 then '1' else 0
end records_sent
from tbl_FAR_PreLoad
where Ship_Dt >= getdate()-1
AND Ship_Dt <= getdate()


Is there a way to say in the job step if its 0 then exit?

I cant seem to find that little piece of code. Any thoughts would be great.

Thanks in advance

Laura

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-05-11 : 04:24:48
As an approach:
Create a first jobstep which executes the following T-SQL statement:
IF (select COUNT(*) from tbl_FAR_PreLoad where Ship_Dt >= getdate()-1 AND Ship_Dt <= getdate())=0
BEGIN
RAISERROR ('Fail!', 16, 1)
END

And if the step fails then quit the job or whatever you want to do then...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2011-05-11 : 14:19:28
Perfect! Thanks so much.

Laura
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-05-12 : 03:25:30
or

IF not exists(select * from tbl_FAR_PreLoad where Ship_Dt >= getdate()-1 AND Ship_Dt <= getdate())
BEGIN
RAISERROR ('Fail!', 16, 1)
END


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-05-12 : 03:45:11
OK, Laura, you should prefer madhi's solution because it will be much faster whenever there is existent data to use in the next job step. I believe that, in most cases, there will be data...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -