Re: Using Cursors
From: peterDavey (peter.davey_at_austin.org.au)
Date: 01/24/05
- Next message: Craig: "RE: Criteria order in WHERE clause"
- Previous message: Aaron Weiker: "Re: sql server name"
- In reply to: peterDavey: "Using Cursors"
- Next in thread: Hugo Kornelis: "Re: Using Cursors"
- Reply: Hugo Kornelis: "Re: Using Cursors"
- Reply: David Portas: "Re: Using Cursors"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 25 Jan 2005 09:47:46 +1100
Thanks to everybody for all the replies so far. It looks like there's a lot
of interest in this topic.
There's no doubt that in Oracle cursors are invaluable. I could indeed do
what I want using set based SQL but it would require 3 - 4 passes through
the data with separate SQL statments to acheive what I want. Using a
cursor, I select the data once, then execute a number of operations for each
record selected - In my Oracle procedures I've found technique this to be
extremely efficient. There are also other stored procedures that I need to
convert that I'm sure I couldn't do without a cursor.
The crux of this question however was not so much about the usefulness or
otherwise of cursors but simply how do I stop the 'feedback' I'm getting for
each record fetched from the cursor. Based on your replies however, I will
compare the performance of the set based SQL solution with the cursor
solution. I'll let you know what I find.
Below is an example of the 'feedback' I'm talking about:
ep_key_e campus_e ur_e
--------------- -------- ---------------
IP01803871 2 743600
(1 row(s) affected)
ep_key_e campus_e ur_e
--------------- -------- ---------------
IP01805187 2 KM3527
(1 row(s) affected)
I can get rid of the "(1 row(s) affected)" by using SET NOCOUNT ON (thanks
GreyAlien007) but the other stuff remains.
cheers
peterDavey
"peterDavey" <peter.davey@austin.org.au> wrote in message
news:eTNJlNgAFHA.3376@TK2MSFTNGP12.phx.gbl...
> G'day,
> I'm moving from Oracle top SQL Server and starting recode my Oracle stored
> procedures into T-SQL. I have a procedure in Oracle that opens a cursor,
> loops through each record and does an update or insert where appropriate.
>
> I've just created my first cursor in T-SQL. It runs OK in Query Analyser
> but the problem is that it in the output pane it displays a separate query
> output for each record returned by the cursor. I don't want to it to
output
> anything other than some summary stats afte it's completed. The code is
> below: I've excluded the SELECT statement because it's BIG.
>
> DECLARE curEpisode SCROLL CURSOR FOR
> SELECT
> some stuff ...
>
> OPEN curEpisode
>
> -- Perform the first fetch.
> FETCH NEXT FROM curEpisode
>
> -- Check @@FETCH_STATUS to see if there are any more rows to fetch.
> WHILE (@@FETCH_STATUS <> -1)
> BEGIN
> -- This is executed as long as the previous fetch succeeds.
> FETCH NEXT FROM curEpisode
> --... do stuff ....
> SET @counter = @counter + 1
> END
>
> CLOSE curEpisode
> DEALLOCATE curEpisode
> SELECT @counter
> GO
>
> Thanks in advance for any help.
>
> cheers
> peterDavey
> Austin Health
> Melbourne
>
>
- Next message: Craig: "RE: Criteria order in WHERE clause"
- Previous message: Aaron Weiker: "Re: sql server name"
- In reply to: peterDavey: "Using Cursors"
- Next in thread: Hugo Kornelis: "Re: Using Cursors"
- Reply: Hugo Kornelis: "Re: Using Cursors"
- Reply: David Portas: "Re: Using Cursors"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|