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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-05-20 : 08:28:24
|
| Georg writes "Hi, Is there any possibility to use stored procedures as part of a select statement?something simple like 1.define a stored procedure 'sp_test'2.sp_test returns the fieldname 'name' from table personexample:select id, exec sp_test from tbpersonthank you for your helpkind regards Georg" |
|
|
dsdeming
479 Posts |
Posted - 2002-05-20 : 08:35:01
|
| NO, that won't work. You could try:INSERT INTO #Temp EXECUTE spand then join #temp to your other table. |
 |
|
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2002-05-20 : 13:01:41
|
| a cleaner alternative might be a derived tableselect t.id,base.namefrom(select id,name from tblperson) as baseinner joinmaintable t on t.id=base.idMike"oh, that monkey is going to pay" |
 |
|
|
gsnk
Starting Member
24 Posts |
Posted - 2002-05-20 : 17:32:40
|
| although mfemenel's solution is the best, you have another option if you're using SS2K: UDFsinside the function you can run the necessary select statement and return a string that could then be used in your select statement. eg:CREATE FUNCTION dbo.fn_test (@Variable INT)RETURN VARCHAR(50)ASBEGINDECLARE @RetVal VARCHAR(50)SELECT @RetVal = Name FROM Person WHERE UserID = @VariableRETURN @RetValEND-----------------------Now you can use that function inline like this:SELECT id, dbo.fn_test(id) AS Name FROM tbpersonThe thing is that in this particular case it doesn't make much sense to use a UDF but there are other cases (date/time manipulation comes to mind) where this comes in VERY handy.;)Gero |
 |
|
|
|
|
|