Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Stored Proc Help Needed
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Stored Proc Help Needed

 Post Reply Post Reply Page  <12
Author
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 03 October 2003 at 3:30am
nice Michael.  I still can't get my head around cursors. Will have to get into them one day.
There have been many, many posts made throughout the world...
This was one of them.
Back to Top
Flamewave View Drop Down
Senior Member
Senior Member
Avatar

Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
Post Options Post Options   Thanks (0) Thanks(0)   Quote Flamewave Quote  Post ReplyReply Direct Link To This Post Posted: 10 October 2003 at 12:02pm

Cursors are actually a nice feature of SQL, once you learn how to use them. This is probally the easiest way to use them, and it has worked every time for me. Assume that @col1 and @col2 are pre-defined variables. You also have to make sure that however many objects your select statement returns is how many objects you catch in the fetch next statement, even if you dont use them.

Begin Tran
Declare cur Cursor For Select column1, column2 From table
Open cur
Fetch Next From cur Into @col1, @col2
While @@Fetch_Status = 0
    Begin
    ...
    If @@ERROR <> 0
        Goto FAIL
    Fetch Next From cur Into @col1, @col2
    End
Close cur
Deallocate cur
Commit Tran
Goto DONE

FAIL:
Rollback Tran
Select -1 As RetVal,'SQL Error' As RetString

DONE:
--done

- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 16 October 2003 at 4:33am

I'm sure they're useful but its been drummed into me to always try and find a 'set-based' method of doing what you want first.

In most cases you can, as long as you think in 'sets', however I have come across cases where its just been too difficult.

On another note, I've been doing the 'MS Programming SQL 2000' course this week, and its more admin and query optimisation than 'programming'.

So if anyones after a pure 'programming' course, don't do this one.

Cheers,
Andrew



Edited by Bunce
There have been many, many posts made throughout the world...
This was one of them.
Back to Top
Flamewave View Drop Down
Senior Member
Senior Member
Avatar

Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
Post Options Post Options   Thanks (0) Thanks(0)   Quote Flamewave Quote  Post ReplyReply Direct Link To This Post Posted: 16 October 2003 at 7:39pm

Yeah, I ussually try to avoid using cursors, but there are some cases where you just can't avoid using them, exspecailly when you more or less write SQL code all day just about for your job. I've always viewed cursors as "evil" and am ussually able to find a way to not use them, but there are those times...

That course sounds like fun, wish I had time to do something like that.

- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.