Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 52337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:53:28+00:00 2026-05-10T16:53:28+00:00

I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,

  • 0

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…

Running Smink's Solution

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:53:29+00:00Added an answer on May 10, 2026 at 4:53 pm

    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 ' + QUOTENAME(@SERVERNAME) + '.' + QUOTENAME(@DBASE) + '.dbo.hbl_clnt_cat     WHERE inactive = ''N''       AND clnt_cat_code = @CODE'  EXECUTE sp_executeSQL @SQL, N'@CODE CHAR(5)', @CODE 

    (Update: Fixed SQL Injection issues.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following stored procedure code when i execute it gives an error saying
I have created a stored procedure shown below ,how will i call this from
I would like to have a stored procedure that will update values in a
I'm using entity framework 4. I have a stored procedure that just updates one
Let's say I have the following two tables: Fields FieldID INT FieldName NVARCHAR(50) Values
I have three tables 'Employees', 'Departments' and 'EmployeesInDepartments' The 'Employees' tables references the 'Departments'
I worked on a project that had the following requirement: TableA is the parent
I have recently started working on a legacy application that has most of its
I have added Full Text Search to my sql server 2008 express database and
I am using a trigger on a table to send an email using sp_send_dbmail.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.