MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 19th, 2004, 08:34 AM
ewon ewon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 1 ewon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post Using results from stored procedure in WHERE condition

Hi,

I'm trying to write a nested stored procedure, with the outer proc named "spAssociate", and inner proc named "spSales".

So far I have created the inner proc

CREATE PROCEDURE spSales
@userID int
AS
SET NOCOUNT ON
SELECT SalesOppID
FROM Sales_Opportunities
WHERE SalesOppCurrentStatus NOT IN ('Sale Lost','Sales Closed','Sale Closed','Unqualified','Deferred','Dropped')
AND OppOwnerUserID = @userID
GO

This was successfully created. I wanted to use the return set in the outer proc, which I tried creating as:

CREATE PROCEDURE spAssociate
(@userID int,
@containerType varchar(100),
@associateType varchar(100))
AS
SET NOCOUNT ON
SELECT AssociateID
FROM AppRelations
WHERE ContainerType=@containerType
AND ContainerID IN (EXECUTE spSales @userID)
AND AssociateType=@associateType
GO

I get an error "incorrect syntax near execute".

How can I use the results from the inner proc for the WHERE condition in my outer proc?

Any help is greatly appreciated. Thanks!

Reply With Quote
  #2  
Old December 21st, 2004, 03:23 PM
SQL Monkey SQL Monkey is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Alabama
Posts: 1 SQL Monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What I would suggest is doing Dynamic SQL here. As in let spSales return a OUTPUT variable or something that has all the ids in a comma seperated list, then in your spAssociate place that whole select statement in a varchar variable and append the return variable value you get when executing the spSales proc... something like below might work as an example.

DEFINE @vcTemp varchar(500)
DEFINE @vcReturnSet varchar(500) --Depending upon how many id's are returned

EXEC spSales @userID, @vcReturnSet
GO

SET @vcTemp = 'SELECT .... IN (' + @vcReturnSet + ')'

EXEC @vcTemp
GO

Now, this is just a rough draft but it should give a possible idea of how to over come this problem using Dynamic SQL.

===========
Brandon Russell
SQL Monkey

Reply With Quote
  #3  
Old December 21st, 2004, 04:48 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,773 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 14 h 30 m 5 sec
Reputation Power: 37
Stay away fro dynamic SQL. It is more efficient to join the tables directly.


Code:
SELECT AssociateID
FROM AppRelations
WHERE ContainerType=@containerType
AND ContainerID IN (
SELECT SalesOppID
FROM Sales_Opportunities
WHERE SalesOppCurrentStatus NOT IN ('Sale Lost','Sales Closed','Sale Closed','Unqualified','Deferred','Dropped')
AND OppOwnerUserID = @userID)
AND AssociateType=@associateType

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Using results from stored procedure in WHERE condition


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway