A: 

You can create a function (instead of a procedure) that returns a table.

CREATE FUNCTION [dbo].[my_function]
(
   @par2     UNIQUEIDENTIFIER, 
   @par2     UNIQUEIDENTIFIER,
   @par3     UNIQUEIDENTIFIER
)
RETURNS @returntable TABLE 
(
   col1 UNIQUEIDENTIFIER,
   col2 NVARCHAR(50),
   col3 NVARCHAR(50)
)
AS
BEGIN
...
END
tpower
A: 

If you don't want to touch your PROCEDURE, you can create a FUNCTION that wraps it and use that wrapper function in queries.

Tomalak
+2  A: 

Change the line:

SET @myDesc = 
  EXEC UDSPRBHPRIMBUSTYPESTARTUP @CODE = @myCode, @DESC = @@tempDesc OUTPUT

to

EXEC UDSPRBHPRIMBUSTYPESTARTUP @CODE = @myCode, @DESC = @tempDesc OUTPUT

And you have missed assigning @DESC in the stored procedure.

SET @SQL = 
  'SELECT @DESC = clnt_cat_desc FROM ' + @SERVERNAME + 
  '.' + @DBASE + '.dbo.hbl_clnt_cat WHERE inactive = ''N''
  AND clnt_cat_code = ''' + @CODE + ''''

EXECUTE sp_executeSQL @SQL, N'@DESC varchar(255) output', @DESC output

You should then use @tempDesc in the next select:

SELECT
  a.clid
  , b.fileid
  , c.usrfullname AS ClientPartner
  , e.usrfullname AS ClientFeeEarner
  , @tempDesc AS ClientPrimaryBusinessType

Also your stored procedure allows for SQL injection around:

SET @SQL = 
  'SELECT clnt_cat_desc FROM ' + @SERVERNAME + 
  '.' + @DBASE + '.dbo.hbl_clnt_cat WHERE inactive = ''N''
  AND clnt_cat_code = ''' + @CODE + ''''

EXECUTE sp_executeSQL @SQL
smink
This does return the result but does not put the value in the <select> statement below.
Lord Future
You should use @tempDesc in the select and not @myDesc. See edited answer.
smink
Smink - I tried your solution and got the following results... Please see embedded image.
Lord Future
You are not assigning @DESC inside [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] stored procedure.
smink
I have updated the answer to include this. Check it up.
smink
The solution works when you change...EXECUTE sp_executeSQL @SQL, N'@DESC varchar(255) output', @DESCto...EXECUTE sp_executeSQL @SQL, N'@DESC varchar(255) output', @DESC = @DESC OUTPUTCould you edit you answer so I can mark it as accepted? Thanks in advance.
Lord Future
Changed Lord Feature. Have forgot the OUTPUT keyword.
smink
No problem Lord Future. Thanks for the upvote.
smink
A: 

Pheww, I was going crazy on how to do this, I needed to make a result from a stored procedure part of a current query and I was having the hardest time doing this. What I did was wrap Procedure with a function and then return the value and that was it.

Thanks Again, Miami Web Design

Miami Web Design