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 4022282
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:27:20+00:00 2026-05-20T10:27:20+00:00

I am having the stored procedure. For that i need to pass the Database

  • 0

I am having the stored procedure. For that i need to pass the Database name as the paramters from another application or another SP. I know the approach of dynamic SQL, something like,

Create procedure mysp(@dbname varchar(20))
as
begin
declare @sql varchar(max)
set @sql='select * from '+@dbname+'.dbo.table'
end

exec mysp 'mydb'

But i dont want the SQL statements as a string. Because in my SP, i have many Sql statements are coming (Not like this only SELECT statement). so can i use,

USE DatabaseName

inside the stored procedure, so that i can use the db name in the sql statements directly without making it as string. Or any other approach is there.
My requirements, only for db name, i dont want the entire the sql statement to be dynamic…
please help me out.
Thanks in advance.

  • 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-20T10:27:21+00:00Added an answer on May 20, 2026 at 10:27 am

    You can add the USE instruction to the dynamic query you are creating. Then you can work with that database’s tables and other objects without the qualifier (within the dynamic query):

    Create procedure mysp(@dbname varchar(20))
    as
    begin
    declare @sql varchar(max)
      set @sql='use '+@dbname;
      set @sql=@sql + ';select ... from dbo.table1';
      set @sql=@sql + ';update dbo.table2...';
      set @sql=@sql + ';insert into dbo.table3...';
      ...
      exec(@sql);
    end
    
    exec mysp 'mydb'
    

    However, while you can do that, it’s not something that you should do, unless you really have to. You are probably trying to avoid creating the same procedure in different DBs, but you may be getting you other problems with this approach, or robbing yourself of some advantages you might otherwise have without resorting to dynamic queries in SPs.

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

Sidebar

Related Questions

I'm working on a stored procedure that uses a cursor on a temporary table
We're having problem with a huge number of legacy stored procedures at work. Do
Having to upgrade a database schema makes installing a new release of software a
I have written an SSIS package that essentially takes data from multiple sources and
We are using Linq-to-SQL with stored Procedures we need help on one of our
I am using a platform (perfectforms) that requires me to use stored procedures for
Having been a PHP developer on LAMP servers for quite a while, is there
Having a heckuva time with this one, though I feel I'm missing something obvious.
Having worked with Classic ASP for about 2 years now by creating a few
Having read the threads Is SqlCommand.Dispose enough? and Closing and Disposing a WCF Service

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.