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

  • SEARCH
  • Home
  • 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 6982193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:15:21+00:00 2026-05-27T18:15:21+00:00

Now I am using one database table to another database stored procedure normally (

  • 0

Now I am using one database table to another database stored procedure normally (database name..table name) but I have dynamically pass the database name how to write the stored procedure pls give me any suggestion

I am writing like this

Select QT.Name,SC.Name as Status,QT.QuoteNumber,QT.PolicyNumber,LC.Name as LineCode,QT.DBAName as DBAName       
  from QMSV3Dev..Quote QT, QMSV3Dev..StatusCode SC, QMSV3Dev..LineCode LC      
  where QT.StatusCode = SC.StatusCode And QT.LineCode = LC.LineCode and       
  QT.DBAName like 'a%' order by QT.Name,QuoteNumber desc  

In above stored procedure QMSV3Dev..Quote, QMSV3Dev is the database name and Quote is table name, this sp executed in another database like test now

Same query pass the database name dynamically

CREATE Procedure [dbo].[usp_GetSearch123]      
(      
 @SearchValue varchar(100),      
 @SearchBy varchar(250),
 @DbName varchar(50)      
)     
AS    
Begin
  Select QT.Name,SC.Name as Status,QT.QuoteNumber,QT.PolicyNumber,LC.Name as LineCode,QT.DBAName as DBAName         
  from  @DbName+'..Quote ' QT,@DbName+'.. StatusCode' SC, @DbName+'..Linecodes' LC      
  where QT.StatusCode = SC.StatusCode And QT.LineCode = LC.LineCode and       
  (QT.Name like @SearchValue+'%' or QT.DBAName like @SearchValue+'%')       
  order by QT.Name,QuoteNumber desc
 end 
  • 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. Editorial Team
    Editorial Team
    2026-05-27T18:15:21+00:00Added an answer on May 27, 2026 at 6:15 pm

    The answer is simpler that you think. For that, you have to use Dynamic SQL.

    It would be something like this:

    CREATE Procedure [dbo].[usp_GetSearch123]      
    (      
     @SearchValue varchar(100),      
     @SearchBy varchar(250),
     @DbName varchar(50)      
    )     
    AS    
    Begin
    
      Declare @cmd varchar(5000)
    
      select @cmd = 
      'Select QT.Name,SC.Name as Status,QT.QuoteNumber,QT.PolicyNumber,LC.Name as LineCode,QT.DBAName as DBAName         
      from ' + @DbName + '..Quote QT, ' + @DbName + '.. StatusCode SC, ' + @DbName+'..Linecodes LC      
      where QT.StatusCode = SC.StatusCode And QT.LineCode = LC.LineCode and       
      (QT.Name like "' + @SearchValue + '%" or QT.DBAName like "' + @SearchValue +'%")       
      order by QT.Name,QuoteNumber desc'
    
      exec(@cmd)
     end 
    

    You create a varchar variable @cmd and put the select on it and afterwards execute it by exec(@cmd).

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

Sidebar

Related Questions

I am using sessions to pass user information from one page to another. However,
I have a stored procedure (sproc) called resetflags. It takes an int parameter. Another
I am using one database like Test(db name) in this database i added 10
I was working with one table and all my program is ok, but now
ADDED: This question is now, I believe, subsumed by this one: Using GNU Screen
Now I'm following one more friend and now I started to learn PLT Scheme(using
I am using Visual Studio to compile and run my ASP.NET website. Now, one
bear with me, I've been using wxPython for all of one day now. Short
I'm now using ListItem() for Json format result, but the generated Json text has
I am now using Emacs 23 with visual-line-mode turned of for text editing but

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.