SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert scalar function to Table Valued func


Convert scalar function to Table Valued func

Author
Message
SQL_Surfer
SQL_Surfer
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1700 Visits: 1099
How can I convert the below function to return table. I am thinking of putting in cross apply rather than calling scalar function as this is getting expensive call.

CREATE function [dbo].[myfunc]
( @a int, @b int, @c money)
returns money
Begin
declare d as money

set d = -(select isnull(sum(d.cola),0)
from (Select distinct c.cola
from tab1 b
join tab2 c
on b.cola = c.colb
where b.cola <= @b and
b.colb = @a) c
join tab3 d
on c.cola = d.colb and
@a <> d.cola)+@c
return d
End
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42768 Visits: 19847
Without more information, I would say you could do it like this.

CREATE function [dbo].[myfunc]
( @a int, @b int, @c money)
returns table
AS
return
select d = (isnull(sum(d.cola),0)+@c) * -1
from (Select distinct c.cola
from tab1 b
join tab2 c
on b.cola = c.colb
where b.cola <= @b and
b.colb = @a) c
join tab3 d
on c.cola = d.colb and
@a <> d.cola





Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search