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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:43:45+00:00 2026-05-15T01:43:45+00:00

I want to use a database which name is stored in a variable. How

  • 0

I want to use a database which name is stored in a variable. How do I do this?
I first thought this would work but it doesn’t:

exec('use '+@db)

That will not change database context

Suggestions anyone?

  • 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-15T01:43:46+00:00Added an answer on May 15, 2026 at 1:43 am

    Unfortunately I don’t know of a direct solution to this one. The nearest working version is:

    DECLARE @db nvarchar(MAX)
    SET @db = 'use DBname'
    Exec sp_executesql @db
    

    but this only changes the context for the length of the procedure call. However, more statements can be included in that call to make use of the context:

    DECLARE @sql nvarchar(MAX)
    SET @sql = 'use DBName SELECT * FROM Table1'
    Exec sp_executesql @sql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice regarding which database I should use. I want to create
First, I don't want to use a database or a local file. Is it
I want to use Access database with query for pivot table in Excel. How
Proviso: I don't want to use a database for the following, please. I'm getting
I want to use a free database to do some databinding (storage) in my
I want to use JDBC to access a MySQL database. What or will an
I want to use jQuery to pull information from my database when I click
I want to use string similarity functions to find corrupted data in my database.
I have normal sqlite database and want to use fts3. As mentioned in http://www.sqlite.org/fts3.html
I created a new mysql database and i want to use foreign keys with

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.