Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to convert this sp into a function

Author  Topic 

sparrow37
Posting Yak Master

148 Posts

Posted - 2009-07-29 : 13:53:06
Hi all,

I want to convert following sp into function so that it returns table with rows.

ALTER PROCEDURE [dbo].[GetUnqualifiedWholesalers]

AS


DECLARE @duration int
DECLARE @minAmount money

-- get Wholesale criteria from wholesalecriteria table
select @duration = duration,
@minAmount = minAmount
from
WholesaleCriteria
where
isActive = 1

select * ,w.*
from users u inner join Wholesalers w
on u.userid = w.userid
where
w.isactive = 1 -- select active wholesaers only
and
u.isactive = 1 -- select active users only
and u.iswholesaler = 1 -- select wholesalers only
and w.IsValidationRequired = 1 -- temporary Wholesalers who must meet wholesalecriteria
and datediff(day,w.wholesalestartdate,getdate()) >= @duration -- difference od wholesalestartdate and today >= @duration
and u.userid in
( -- select all customers who have purchased more than minamount between their wholesalestartdate and today
select O.Customerid
from orders o inner join orderdetails od
on o.Orderid = od.Orderid
inner join wholesalers w
on w.userid = o.customerid
where o.DateCreated
--between dateadd(day, -@Duration, getdate()) and getdate()
between w.wholesalestartdate and getdate()
and datediff(day,w.wholesalestartdate,getdate()) >= @duration
and w.ReminderSentOn >= DATEADD(DAY, 1 , GETDATE())
group by O.customerid
having sum(od.subtotal) < @minAmount)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-07-29 : 14:59:54
I would not recommend this for performance reasons. Leave it as a stored procedure.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

sparrow37
Posting Yak Master

148 Posts

Posted - 2009-07-29 : 15:01:45
i need to use it in a where clause thats why i am trying to convert it to function.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-07-29 : 15:02:50
You don't need a function for that. Put the output of the stored procedure into a temporary table and then use the #temp table in your query.

INSERT INTO #temp
EXEC spName...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-07-29 : 15:24:56
If you wanted to try it just to see how performance is then your best bet might be a in inline, single statement, table valued UDF. This code makes an assumption that [WholeSaleCriteria] only has one row where isActive=1. also, use an explicit column list in the SELECT clause and make sure your column names are unique.

obviously this is untested:

create function UnqualifiedWholesalers()
returns table
as
return

select u.userid
,u.isactive as isActiveUser
,w.isactive as isActivewholesaler
,u.iswholesaler
,w.IsValidationRequired
from users u
inner join Wholesalers w
on u.userid = w.userid
inner join WholesaleCriteria wc
on wc.isActive = 1
where w.isactive = 1 -- select active wholesaers only
and u.isactive = 1 -- select active users only
and u.iswholesaler = 1 -- select wholesalers only
and w.IsValidationRequired = 1 -- temporary Wholesalers who must meet wholesalecriteria
and datediff(day,w.wholesalestartdate,getdate()) >= wc.duration -- difference od wholesalestartdate and today >= @duration
and u.userid in
( -- select all customers who have purchased more than minamount between their wholesalestartdate and today
select O.Customerid
from orders o
inner join orderdetails od
on o.Orderid = od.Orderid
inner join wholesalers w
on w.userid = o.customerid
inner join WholesaleCriteria wc
on wc.isActive = 1
where o.DateCreated between w.wholesalestartdate and getdate()
and datediff(day,w.wholesalestartdate,getdate()) >= wc.duration
and w.ReminderSentOn >= DATEADD(DAY, 1 , GETDATE())
group by O.customerid
having sum(od.subtotal) < max(wc.minAmount) --min/max doesn't matter - only one value
)

go

select * from UnqualifiedWholesalers() where isValidationRequired = 0


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -