I have created the following stored procedure..
CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) , @DESC VARCHAR(255) OUTPUT ) AS DECLARE @SERVERNAME nvarchar(30) DECLARE @DBASE nvarchar(30) DECLARE @SQL nvarchar(2000) SET @SERVERNAME = Convert(nvarchar, (SELECT spData FROM dbSpecificData WHERE spLookup = 'CMSSERVER')) SET @DBASE = Convert(nvarchar, (SELECT spData FROM dbSpecificData WHERE spLookup = 'CMSDBNAME')) 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 RETURN
This procedure is used in many different databases and many different servers and is written as dynamic SQL to simplify maintenance. The procedure also runs on a different server than the one the procedure points to.
I want to use the output of this procedure as a value in a table…
DECLARE @clid BIGINT DECLARE @fileid BIGINT DECLARE @myCode CHAR(5) DECLARE @myDesc VARCHAR(255) DECLARE @@tempDesc VARCHAR(255) SET @clid = 1831400022 SET @fileid = 2072551358 SET @myCode = (SELECT _clientPrimBusinessType FROM udbhextclient WHERE clid = @clid) SET @myDesc = EXEC UDSPRBHPRIMBUSTYPESTARTUP @CODE = @myCode, @DESC = @@tempDesc OUTPUT ---------------------------------------------------------------------------- SELECT a.clid , b.fileid , c.usrfullname AS ClientPartner , e.usrfullname AS ClientFeeEarner , @myDesc AS ClientPrimaryBusinessType FROM dbclient a INNER JOIN dbFile b ON a.clid = b.clid INNER JOIN dbuser c ON a.feeusrid = c.usrid INNER JOIN udbhextclient d ON a.clid = d.clid INNER JOIN dbuser e ON d._ClientFeeEarner = e.usrid WHERE a.clid = @clid AND b.fileid = @fileid
I know this is the incorrect syntax, but you can see what I am trying to achieve this without resorting to temporary tables as this would mean maintenance across 30 different servers with 3 to 5 databases on each.
Smink – Tried your solution and got the following results…

Change the line:
to
And you have missed assigning
@DESCin the stored procedure.You should then use
@tempDescin the next select:Also your stored procedure allows for SQL injection around:
(Update: Fixed SQL Injection issues.)