473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Launch storedquery for each record

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
Nov 21 '07 #1
6 1483
On Wed, 21 Nov 2007 11:49:33 -0800 (PST), Winbug wrote:
>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
Nov 21 '07 #2
Hugo Kornelis (hu**@perFact.REMOVETHIS.info.INVALID) writes:
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, es****@sommarskog.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
Nov 21 '07 #3
On Wed, 21 Nov 2007 22:22:57 +0000 (UTC), Erland Sommarskog wrote:

(snip)
>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
Nov 21 '07 #4
Hugo Kornelis (hu**@perFact.REMOVETHIS.info.INVALID) writes:
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.
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.
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, es****@sommarskog.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
Nov 22 '07 #5
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
Nov 22 '07 #6
It' all good.
Cursors worked and did the job.

Roger
Nov 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Dew | last post by:
hi, I want to interogate the Quick Launch folder, looking at each icon and getting the image, and target that it fires when clicked. I can get to the folder and create a filelist of the icons, but...
8
by: Paul | last post by:
I have looked and looked for this info. All the I/O examples I've found either explicity use a file name or use the OpenFileDialog. When you drag a MS Word document over MS Word, it launches and...
7
by: dinamointer | last post by:
Could you help me in this problem: I want to launch an exe file(executable jar file) from a web page. I use jsp...and i cannot use vbScript...? could u tell me how should i do it? Thanks
0
by: Marcelo | last post by:
Hi, I have the following need: I list a table from an access database but cannot interact with each record. At the html page I display all records, one field (field1) from and 3 buttons for each...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am deploying vb2005 apps using Click Once deployment. This works fine. The problem is that the user needs to be able to launch a 2nd app from the 1st app, but the application...
1
by: ben.brearley | last post by:
Hi All, Does anyone know of any way to launch C# forms from an MS Access application? Weird question I suppose, but the reason is that I am wanting to rewrite a MS Access application into a...
4
by: QntmPg | last post by:
Hi all, I have read through what I could find on previous questions regarding using the OpenArgs property, but I'm still not able to get my form to open correctly. I'm not sure exactly where the...
2
by: angi35 | last post by:
I hope this is an easy question for someone out there. In Access 2000…I have a MainForm with a tab control (MAIN TABS) with 7 tabs. Within each tab is a SubForm. Within each SubForm is a tab...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.