Unix Technical Forum

Passing a specific cursor record to a function

This is a discussion on Passing a specific cursor record to a function within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, Is it possible? Can I select a specific record of the cursor to be sent to a seperate ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:14 AM
TinTin
 
Posts: n/a
Default Passing a specific cursor record to a function

Hello,

Is it possible? Can I select a specific record of the cursor to be
sent to a seperate function to do all the computations etc.?

Regards,
VS
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:14 AM
John Bell
 
Posts: n/a
Default Re: Passing a specific cursor record to a function

Hi

You will have to pass each as a separate variable or possibly use a
(temporary) table. If you can rewrite the cursor to be a set function you
will usually get much better performance.

John

"TinTin" <lalalulu24@yahoo.com> wrote in message
news:2d5425d1.0406151234.3925efae@posting.google.c om...
> Hello,
>
> Is it possible? Can I select a specific record of the cursor to be
> sent to a seperate function to do all the computations etc.?
>
> Regards,
> VS



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:15 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Passing a specific cursor record to a function

TinTin (lalalulu24@yahoo.com) writes:
> Is it possible? Can I select a specific record of the cursor to be
> sent to a seperate function to do all the computations etc.?


You can pass a cursor varible to stored procedure, but I am not sure that
functions accept cursor variables.

In any case, cursors is not something you should use that often. As I said
in my other posting, work set-based whenever possible.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:15 AM
TinTin
 
Posts: n/a
Default Re: Passing a specific cursor record to a function

Hi
John: I think I will buy the Temporary Table suggestion. Thanks!

Erland: I am not too sure about what you mean by "work set-based".
There might be an easier alternative to what I am doing. Could you
precisely refer to me a specific area; or topic which I should read?

Regards!


Erland Sommarskog <esquel@sommarskog.se> wrote in message news:<Xns950A2ABF8BDEYazorman@127.0.0.1>...
> TinTin (lalalulu24@yahoo.com) writes:
> > Is it possible? Can I select a specific record of the cursor to be
> > sent to a seperate function to do all the computations etc.?

>
> You can pass a cursor varible to stored procedure, but I am not sure that
> functions accept cursor variables.
>
> In any case, cursors is not something you should use that often. As I said
> in my other posting, work set-based whenever possible.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 04:15 AM
John Bell
 
Posts: n/a
Default Re: Passing a specific cursor record to a function

Hi

You would have to post the DDL (Create table statements etc), Example Data
as Insert statements and expected output along with your stored procedure
definition, so that we have a better idea what you are trying to do.

John

"TinTin" <lalalulu24@yahoo.com> wrote in message
news:2d5425d1.0406160504.66185536@posting.google.c om...
> Hi
> John: I think I will buy the Temporary Table suggestion. Thanks!
>
> Erland: I am not too sure about what you mean by "work set-based".
> There might be an easier alternative to what I am doing. Could you
> precisely refer to me a specific area; or topic which I should read?
>
> Regards!
>
>
> Erland Sommarskog <esquel@sommarskog.se> wrote in message

news:<Xns950A2ABF8BDEYazorman@127.0.0.1>...
> > TinTin (lalalulu24@yahoo.com) writes:
> > > Is it possible? Can I select a specific record of the cursor to be
> > > sent to a seperate function to do all the computations etc.?

> >
> > You can pass a cursor varible to stored procedure, but I am not sure

that
> > functions accept cursor variables.
> >
> > In any case, cursors is not something you should use that often. As I

said
> > in my other posting, work set-based whenever possible.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 04:16 AM
Erland Sommarskog
 
Posts: n/a
Default Re: Passing a specific cursor record to a function

TinTin (lalalulu24@yahoo.com) writes:
> Erland: I am not too sure about what you mean by "work set-based".
> There might be an easier alternative to what I am doing. Could you
> precisely refer to me a specific area; or topic which I should read?


Rather than writing:


DECLARE @price money,
@qty int,
@total money,
@orderid int,
@prev_orderid int

DECLARE order_total_cur INSENSITIVE CURSOR FOR
SELECT orderid, price, qty
FROM order_details
ORDER BY orderid

OPEN order_total_cur
SELECT @total = 0
WHILE 1 = 1
BEGIN
FETCH order_total_cur INTO @orderid, @price, @qty
IF @@fetch_status <> 0
BREAK

IF @prev_orderid IS NOT NULL AND @orderid <> @prev_orderid
BEGIN
UPDATE orders
SET total = @total
WHERE orderid = @prev_orderid

SELECT @total = 0
END

SELECT @total = @total + @price * @qty, @prev_orderid = @orderid
END

DEALLOCATE order_total_cur

IF @orderid IS NOT NULL
BEGIN
UPDATE orders
SET total = @total
WHERE orderid = @orderid
END

You write:

UPDATE orders
SET total = od.total
FROM orders o
JOIN (SELECT orderid, total = sum(qty * price)
FROM orderdetails
GROUP BY orderid) AS od ON o.orderid = od.orderid

Not only is this more concise and less error-prone to write, the
difference in performance could be magnirute if there are many
rows in the table.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:50 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com