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

The Archive Base Latest Questions

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

I inherited a project where each ‘customer’ has its own database. There are hundreds

  • 0

I inherited a project where each ‘customer’ has its own database. There are hundreds of databases. Stored procedures are currently not being used.

What are the best practices for consuming data here? Do I keep my stored procedures in the “master” database and use dynamic SQL to muck with data? It seems like there should be a much better way. I don’t want to have a job running to push stored procedures around hundreds of DBs to keep all the stored procedures in sync.

This dynamic SQL is working, but I want a better way.

CREATE PROCEDURE [Users_SELECT] 
@DataBase nvarchar(20),
@UserID uniqueidentifier
AS
BEGIN
DECLARE @sql nvarchar(max)
SET @sql = ''
SET @sql += 'SELECT * FROM ' + @DataBase + '.dbo.Users u '
SET @sql += 'WHERE u.UserID=@UserID '
EXEC sp_executesql @sql, N'@UserID uniqueidentifier', @UserID
END

I tried EXEC sp_executesql 'USE ' + @DataBase + '; GO' then running a SELECT but I couldn’t get that working.

  • 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-28T16:46:45+00:00Added an answer on May 28, 2026 at 4:46 pm

    I figured it out. This will probably make SQL experts extremely mad, but I’m happy with the solution.

    I created my stored procedures in ‘master’. I know this is usually a no-no. I then used

    USE master
    EXEC sys.sp_MS_marksystemobject [sp_Users_SELECT]
    GO
    

    to mark each sproc as a system object. This way they can be called while under a different context. I can then do this:

    USE DataBase200
    EXEC sp_Users_SELECT
    

    and it will run in the context of the DataBase200 db, without having to actually deploy the sproc to that database. It is working great so far. Remember that you have to prefix your stored procedures names with sp_ in order for them to be recognized as system objects.

    I hope this helps someone.

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

Sidebar

Related Questions

I have inherited a project that has class libraries written in VB.NET, some of
I have inherited a project, that provides an order form to a customer. The
I've inherited a project where all the private variables, and there are thousands, are
I've inherited a flash project (my first) that has many existing symbols in the
I'm attempting to modify an inherited project that has a convoluted process of displaying
I have inherited a project that has an awkwardly big interface declared (lets call
I have inherited a Visual Studio project that contains hundreds of files. I would
I'm not overly familiar with Tomcat, but my team has inherited a complex project
I just inherited a large web project using SVN for VCS. There are no
I have inherited a project that has a large number of source files. 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.