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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:30:19+00:00 2026-05-12T21:30:19+00:00

I am doing a code review with a client. It is a proc on

  • 0

I am doing a code review with a client. It is a proc on SQL Server 2008. He wants to do everything in SQL and not a process. He is looping through a list of catalogs and then running a proc in each one:

DECLARE CatalogList CURSOR FOR 
SELECT 'EXEC ' + c.CatalogName+ '.dbo.procInEachDb '
FROM CatalogList

FETCH NEXT FROM CatalogList INTO @SQL
WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC( @SQL)
    --PRINT @SQL
    FETCH NEXT FROM CatalogList INTO @SQL
END

I pointed out that a deadlock in one db would kill the entire process. His point is that this is code that runs every five minutes and it will just run next time.

This code smells bad, but I cannot give specific reasons. Am I wrong about this being a problem?

  • 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-12T21:30:19+00:00Added an answer on May 12, 2026 at 9:30 pm

    That really depends on what procInEachDb does. I can understand the use case where you have the same code in each database that you need to execute independently because it relies on locally-scoped objects, manipulates data, pushes data elsewhere, etc. Not a lot of things can be done in multiple databases from a single operation, especially if the set of databases is dynamic (arguably with a static set, you could write UNIONs across databases etc., but you still couldn’t write). But I’d really try to understand from the client what the procedure does and why it is broken out this way. It may very well be the best way to do what the client needs to do, and in that case there is no bad smell at all, except that maybe it could use better error handling. =)

    I manage one system where we have 500+ roughly identical databases on an instance, and we need to run the same set of regularly scheduled tasks in each one. I don’t want to create 500 Agent jobs, and I don’t want to create a job with 500 steps, so instead I created a stored procedure with some tweaks that behaves like sp_MSForEachDB (which is essentially the same as the code you show above). I re-use this same procedure for a lot of commands that I want to run against all of the databases. The procedure also takes arguments that allow exclusions, etc.

    Yes I could have done something similar with service broker, but I find that for some things I need more visibility and control over what is actually going on. And of course doing manual one-time things is easy with tools like SQL Farms Combine or Red-Gate’s multi-script tool.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hmm.. no one answered this question. I realized that there… May 12, 2026 at 9:30 pm
  • Editorial Team
    Editorial Team added an answer You would need to save the hashed password (be it… May 12, 2026 at 9:30 pm
  • Editorial Team
    Editorial Team added an answer If your code points are contiguous, just make a big… May 12, 2026 at 9:30 pm

Related Questions

I'm building a framework and want developers who build with it to have the
I am currently doing a code review and the following code made me jump.
OK I keep getting this error after about 3-4 minutes of churning: Timeout expired.
I have large batches of XHTML files that are manually updated. During the review
I am trying to download a file over HTTPS and I just keep running

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.