dbTalk Databases Forums  

Launch storedquery for each record

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Launch storedquery for each record in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Winbug
 
Posts: n/a

Default Launch storedquery for each record - 11-21-2007 , 01:49 PM






hi
I have a scenario where I have created a stored procedure wich task is
to search in my database with certain criterias. For each record from
this search, I want to run a couple of other stored procedure.


Here is some code:

decare $myid nvarchar(15)

select id, desc, customer from project where customer = 'cust1'
------------------------------------------
--HERE IS CODE MISSING--
------------------------------------------
--for each of the records returned, I want to run 4 stored procedures
with the id as parameter.
begin
execute StoredProcedure1 $myid
execute StoredProcedure2 $myid
execute StoredProcedure3 $myid
execute StoredProcedure4 $myid
end

The problem is I don't know how to get hold of the result set and loop
through. Maybe you can help me.

regards
Roger

Reply With Quote
  #2  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Launch storedquery for each record - 11-21-2007 , 03:34 PM






On Wed, 21 Nov 2007 11:49:33 -0800 (PST), Winbug wrote:

Quote:
hi
I have a scenario where I have created a stored procedure wich task is
to search in my database with certain criterias. For each record from
this search, I want to run a couple of other stored procedure.


Here is some code:

decare $myid nvarchar(15)

select id, desc, customer from project where customer = 'cust1'
------------------------------------------
--HERE IS CODE MISSING--
------------------------------------------
--for each of the records returned, I want to run 4 stored procedures
with the id as parameter.
begin
execute StoredProcedure1 $myid
execute StoredProcedure2 $myid
execute StoredProcedure3 $myid
execute StoredProcedure4 $myid
end

The problem is I don't know how to get hold of the result set and loop
through. Maybe you can help me.

regards
Roger
Hi Roger,

From a performance point of view, this is not a good way to build your
application. You should rewrite the four stored procedures to operate on
all qualifying data at once, as SQL Server is optimized for processing
set-based queries.

However, there may be reasons that this is not feasible. The cost of
rewriting the procs may be high, and if your amount of data is low,
performance might not be a consideration. In that case, you can use a
cursor to process the results of a query row by row. There are many
examples in Books Online, so if you just use the index to find DECLARE
CURSOR, you should be all set.

If performance does matter and you still want to use a cursor, than you
might wish to read my recent blog posts on the performance effects of
various cursor options. You can find it at
http://sqlblog.com/blogs/hugo_kornel...n-options.aspx

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Launch storedquery for each record - 11-21-2007 , 04:22 PM



Hugo Kornelis (hugo (AT) perFact (DOT) REMOVETHIS.info.INVALID) writes:
Quote:
If performance does matter and you still want to use a cursor, than you
might wish to read my recent blog posts on the performance effects of
various cursor options. You can find it at
http://sqlblog.com/blogs/hugo_kornel...urious-cursor-
optimization-options.aspx
As the hour is late, I didn't have the time to read it all, but I will
save that for later. But I found the conclusions interesting. I always
go for STATIC cursors myself, mainly because then I know that nothing
unexpected will happen. (Actually I really prefer INSENSITIVE as that is
ANSI, but alas it cannot be combined with LOCAL.)

I also note that WHERE CURRENT OF is still bad. I tried WHERE CURRENT OF
when I first got play with SQL 6.0, but ran into permission issues, and
I also noticed a scan in the query plan. And since updating through the
PK is dead simple, why bother with anything else?

One battle I have to fight in my shop is with colleagues who think that a
"poor man's cursor" is better. And maybe sometimes it is. But having a
loop where you do SELECT MIN() on a non-indexed temp table with 100000
rows is definitely not.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Launch storedquery for each record - 11-21-2007 , 05:05 PM



On Wed, 21 Nov 2007 22:22:57 +0000 (UTC), Erland Sommarskog wrote:

(snip)
Quote:
One battle I have to fight in my shop is with colleagues who think that a
"poor man's cursor" is better. And maybe sometimes it is. But having a
loop where you do SELECT MIN() on a non-indexed temp table with 100000
rows is definitely not.
Hi Erland,

Maybe it is, but I doubt it. After investigating the effect of options,
I wouldn't be surprised to find a way to beat a cursor with the default
options, since they are SLOW - but I have yet to see a "poor man's
cursor" that outperforms a _properly optimized_ cursor.

If your colleagues ever show you a way that they think beats a cursor,
please share it with me. Might make neat blog fodder.

Hmmm, maybe I'll do a sequel to the cursor episode anyway. The
misunderstanding is common enough to warrant some attention on my blog.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Launch storedquery for each record - 11-22-2007 , 02:18 AM



Hugo Kornelis (hugo (AT) perFact (DOT) REMOVETHIS.info.INVALID) writes:
Quote:
Maybe it is, but I doubt it. After investigating the effect of options,
I wouldn't be surprised to find a way to beat a cursor with the default
options, since they are SLOW - but I have yet to see a "poor man's
cursor" that outperforms a _properly optimized_ cursor.
Yes, just saying DECLARE cur CURSOR may lead to problem. I don't have
had much problems with performance on SQL 2000 and later, but that is
mainly because I was burnt enough on 6.5. These days I make my cursors
STATIC/INSENSITIVE as a matter of routine.

But another issue that I've seen people run into with dynamic cursors is
that rows keeps coming back to you, so that your cursor never terminates.

Quote:
If your colleagues ever show you a way that they think beats a cursor,
please share it with me. Might make neat blog fodder.
Well, if you index your loop column it's a different matter, and while
have to admit that there have been occasions I've written that sort of
loops myself. But that is probably maninly due to that a cursor declaration
and all takes up some more space in the code. But from a performance point
of view, there is a certain overhead in setting up a static cursors, since
rows has to be copied. But if you have that many rows to make this overhead
noticable, then you are probably in dire need of a set-based solution.

Quote:
Hmmm, maybe I'll do a sequel to the cursor episode anyway. The
misunderstanding is common enough to warrant some attention on my blog.
Yes, people see "don't use cursors" and then implement their loops in
a different way, and thus go from bad to worse.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #6  
Old   
Winbug
 
Posts: n/a

Default Re: Launch storedquery for each record - 11-22-2007 , 03:39 PM



Thanks Hugo
I'll have a look at cursors.
Since my job is only going to run once a day I am not concerned about
performance. The queries are broken up because they are used here and
there in other stored procedures.

Roger

Reply With Quote
  #7  
Old   
Winbug
 
Posts: n/a

Default Re: Launch storedquery for each record - 11-23-2007 , 10:01 AM



It' all good.
Cursors worked and did the job.

Roger

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.