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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to use sore procedure to do it?

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-02-09 : 12:00:53
I wrote a sql statement as bellow:

select * from tblOrder where OrderID in (select * from tblNewOrder where Order_City = 'New York')

It works fine. Now, I want to change to store procedure. How to code it?

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2006-02-09 : 12:14:17
First thing to do is to change it from Select * to an actual field list, just to get out of the habit before you learn to use select * all the time..

Then you just:


create procedure usp_NewYorkOrders
as
select <fieldlist> from tblOrder where OrderID in (select OrderID from tblNewOrder where Order_City = 'New York')


Then to call it just run


exec usp_NewYorkOrders

Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-02-09 : 12:26:44
Thank you.

If I use two sp, like bellow, is it fast than one sp?

sp1: select <fieldlist> from tblOrder where OrderID in (sp2 output)
sp2: select OrderID from tblNewOrder where Order_City = 'New York'
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-02-09 : 13:07:18
how about a single sp that does this?

select <a.fieldList>
from tblOrder a
join tblNewOrder b
on b.orderid = a.orderid
where b.order_city = 'New York'


Be One with the Optimizer
TG
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-02-09 : 14:06:43
Can there be duplicates in tblNewOrder? If so it will return duplicate rows from tblOrder (which RickD's solution avoids)

Seems unlikely with reasonably normalised data etc., but just thought I'd mention it!

Kristen
Go to Top of Page
   

- Advertisement -