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 2005 Forums
 Transact-SQL (2005)
 Can we use Stored Procedure in a Select Statement?

Author  Topic 

savvy
Starting Member

48 Posts

Posted - 2007-03-24 : 07:59:31
Can I use a Stored Procedure value in a Select Statement.
For Example:
Lets Say sp_SelectSortID
returns only one integer value which is 2442
So, when I do 'EXEC sp_SelectSortID' it gives

SortID
2442

But if I want to use this in Select statement,
For Eg: I want something like " Select * from Credits Where SortID= (Exec sp_SelectSortID)

is it actually possible ? This question was asked to me in my interview yesterday. I searched in google but didn't get relevant results. Any ideas will be of great help to me

Thanks in Advance



Savvy

bpgupta
Yak Posting Veteran

75 Posts

Posted - 2007-03-24 : 08:49:50
U can't use the Stored Proc in the Select statement.to use the same things..u assigne to out of SP to one variable then use that variable in the select statement.

Thanks,
Bali
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-03-24 : 11:05:45
have a look at OUTPUT params in procs in BOL.


www.elsasoft.org
Go to Top of Page

savvy
Starting Member

48 Posts

Posted - 2007-03-24 : 15:05:45
wow! Thank you very much guys for your answers
I done a little bit of research and came out with a working example which works on my table

Example:
***************
CREATE PROCEDURE sp_Temp
(
@creditID int,
@sortID int OUTPUT
)
AS
Select @sortID=SortID from SelectedCredits where CreditID=@creditID
GO

****************

DECLARE @TempSortID int
Exec sp_Temp
@creditID=111,
@sortID=@TempSortID OUTPUT
Select TheCount = @TempSortID
*************

I am giving this example if incase someone else is searching for the same
This will be bit helpful for them

Thank you very very much guys for your help


Savvy
Go to Top of Page
   

- Advertisement -