March 18, 2015 at 8:57 am
I have a stored procedure with several insert into statements. On occasion one of the insert into queries doesn't return any data. What is the best way to test for no records then, skip that query?
March 18, 2015 at 9:06 am
Well the way you test for a query not returning/returning records is by running the query..... so what exactly is the problem with running the insert into if no records are returned?
March 18, 2015 at 9:33 am
There are several queries that produce a result. The result gets messed up if this query is not skipped.
March 18, 2015 at 9:49 am
Well without knowing more about what you're trying to do it's hard to say but have you tried checking @@ROWCOUNT after your insert into
INSERT INTO MY_TABLE(....) SELECT ......
IF @@ROWCOUNT > 0
BEGIN
/*Do your stuff here*/
END
March 18, 2015 at 10:04 am
Thanx. I'll give it a try.
March 18, 2015 at 10:23 am
Another option, according to what I understood.
IF EXISTS(SELECT 1 FROM MyQuery)
INSERT INTO MyDestination( mycolumns)
SELECT mycolumns FROM MyQuery
The exists will stop at the first returned row, so it should be fast.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply