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

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

The Archive Base Latest Questions

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

In a single SQL Server instance, I have the following databases: Headquarters Branch001 Branch002

  • 0

In a single SQL Server instance, I have the following databases:

  • Headquarters
  • Branch001
  • Branch002
  • Branch003

…

  • Branchnnn

In each Branch DB is a procedure called usp_ComputeDailySales that does some computation and writes daily sales figures to the Headquarters DB. How do I write a single stored procedure in the Headquarters DB to execute usp_ComputeDailySales in all Branch DBs?

I could write something like the code below but I think this is not the way to go.

USE Branch001
EXEC usp_ComputeDailySales

USE Branch002
EXEC usp_ComputeDailySales

...

I wanted some way to execute the procedure for all Branch DBs all at the same time.

  • 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-12T08:43:45+00:00Added an answer on May 12, 2026 at 8:43 am

    This is not dynamic SQL, but relies on something like deferred name resolution.

    EXECUTE, see @module_name_var parameter

    DECLARE @sql varchar(300)
    SELECT
        [name] + '..usp_ComputeDailySales' AS Cmd
    INTO #List
    FROM
        sys.databases d WHERE d.[name] LIKE 'branch%'
        OR --Thanks Mitch
        d.[name] = 'Headquarters'
    
    WHILE EXISTS (SELECT * FROM #List)
    BEGIN
        SELECT TOP 1 @SQL = Cmd FROM #List
        EXEC @SQL
        DELETE #List WHERE Cmd = @SQL
    END
    

    Edit, after KM’s comment

    Yes, there is a difference but I’ll clarify:

    EXEC (@SQL) means @SQL could be ‘SELECT * FROM foo‘ OR ‘DELETE User WHERE 1=1' ;.)

    EXEC @SQL means @SQL is an object name. So I should have used @Obj

    So, you could do this:

    DECLARE @Obj varchar(200)
    SELECT @Obj = 'dbo.uspCustomHook'
    IF OBJECT_ID(@Obj) IS NOT NULL
        EXEC @Obj @p1 = 1, @p2 = 'bob', ...
    

    It avoids deferred name resolution errors. When the batch/proc is compiled, there is no error if dbo.uspCustomHook does not exist.

    I’ve not used the technique since SQL 2000, and I don’t know if SQL 2005 (statement level recompilation) is clever enough to work out if @Obj should be be run because it does not exist so it does not try to compile the EXEC statement…

    It’s also useful for cross database, same instance queries. You can configure prd-prod, dev-dev etc without relying on linked servers or hard coded databases names.

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

Sidebar

Ask A Question

Stats

  • Questions 173k
  • Answers 173k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer solution : http://pylonshq.com/docs/en/0.9.7/configuration/ this is how it works-> $ paster… May 12, 2026 at 2:37 pm
  • Editorial Team
    Editorial Team added an answer Check out Cassini. It's a web server that has been… May 12, 2026 at 2:37 pm
  • Editorial Team
    Editorial Team added an answer File, recent projects. If it's only showing 4 or 5… May 12, 2026 at 2:37 pm

Related Questions

We are getting the following error on a certain database occasionally under moderate load.
I've already checked out the question Deleting duplicate records using a temporary table and
I'm after a simple stored procedure to drop tables. Here's my first attempt: CREATE
I am trying to test a proof of concept that I can run a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.