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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:06:05+00:00 2026-05-23T12:06:05+00:00

so I have a sproc in a db.. lets call this db A .

  • 0

so I have a sproc in a db.. lets call this db A. This db makes use of tables (t1, t2) in another db. Lets call this db B.

okay, so the way i call it right now is: A.dbo.My_Proc but i get another error:

Invalid object name ‘dbo.t1’.

so how i tried supplying a parameter. In my Sproc i do, select * from @dbname.dbo.t1
however that results in an error. I can’t put the sproc in db B.

While it is sufficient to hardcode it (if there is a way), db B changes every year, so it would be nice to “supply” a database.

I tried using use B; go but it gives me error saying can’t have that in a sproc.

  • 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-23T12:06:06+00:00Added an answer on May 23, 2026 at 12:06 pm

    You could create a synonym:

    EDIT: I see now that the synonym is needed for the table, not the proc. Got them switched. So you could create a synonym in database A for the table in database B:

    USE A;
    CREATE SYNONYM dbo.t1 FOR B.dbo.t1;
    

    Then your procedure in A could simply say:

    SELECT * FROM dbo.t1;
    

    Without having to manually supply the database name at all, the query knows (based on the synonym) to get the data from the table in database B. When the database B changes to C, you can simply:

    DROP SYNONYM dbo.t1;
    CREATE SYNONYM dbo.t1 FOR C.dbo.t1;
    

    If you used “real” database names in your narrative as opposed to arbitrary A/B names, it might lead to easier comprehension. Just a suggestion. 🙂

    /EDIT

    The other option is to pass in the database name and construct via dynamic SQL. E.g. instead of select * from @dbname.dbo.t1 (which will never work), you could do:

    DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM ' + QUOTENAME(@dbname) + '.dbo.t1;';
    EXEC sp_executesql @sql;
    

    But if this other database name really only changes once a year, I suggest that the synonym route is better overall.

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

Sidebar

Related Questions

I have a SPROC let's call it: AccountExists , with parameter AccountID . I
I have to following sproc: USE [CW] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER
I have a SPROC that runs every hour. This SPROC calculates the popularity of
I have a sproc that returns a single line and column with a text,
I have seen various rules for naming stored procedures. Some people prefix the sproc
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have an sproc in SQL Server 2008. It basically builds a string, and
Ok so, I'm getting this error when I try to use the asset pipeline.
Okay I have a queswtion about returning the recordset from multiple stored procedures. If
I have a stored procedure (sproc A) which is syntactically correct. So when I

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.