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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:26:09+00:00 2026-05-19T03:26:09+00:00

I have several databases I’m managing, and each needs a copy of a particular

  • 0

I have several databases I’m managing, and each needs a copy of a particular stored procedure.

The problem is, the stored procedure insert into a table for all of the databases except one of them. For that one database, the table is a partitioned view and is not updatable, and I don’t need to insert.

I’ve tried to do something like the following:

CREATE PROCEDURE st_doit AS
    -- Do lots of other stuff...
    IF(DB_NAME() <> 'db3') BEGIN
       INSERT INTO mytable...;
    END

I still get an error message when attempting to execute the stored procedure in db3 because of the INSERT statement, even though in that database, the insert won’t actually be executed. Wrapping the INSERT in a TRY...CATCH block didn’t help either.

For ease of maintenance, I would REALLY like to avoid having a special copy of the procedure in db3. Usually I wouldn’t put an IF statement like this in a procedure, but in this particular case, it really is the best solution, and it won’t grow over time into a nest of other special cases over time.

So, how do I force SQL Server to just execute the stored procedure and only generate an error if I actually try to insert into the table?

Edit: Since using dynamic SQL was a bit too messy for me, my end solution was to keep the IF block as a trap, but to comment out the offending block within db3. Not ideal, but works. Fortunately I won’t be changing this SP often, but I wanted a “complete” copy of it in every database.

  • 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-19T03:26:09+00:00Added an answer on May 19, 2026 at 3:26 am

    You’d have to “hide” the INSERT from SQL Server using dynamic SQL.

    CREATE PROCEDURE st_doit AS
        -- Do lots of other stuff...
        IF(DB_NAME() <> 'db3') BEGIN
           EXEC SP_EXECUTESQL N'INSERT INTO mytable...';
        END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several databases that each have a table called, say, products . One
I have several times ran into the same problem, and I would like to
I'm working with a SQL server and several databases. Each database may have many
In our project we have several production databases and many devs. Each production database
I have several databases where the transaction log (.LDF) is many times larger than
I have several databases (SqlServer 2005) on the same server with the same schema
We are moving our database server to a bigger box. I have several databases
At work, we have several applications with databases in a centralized SQL server. Whenever
We have several SQL Server 2000 databases (I know, we need to upgrade) that
I have a ~23000 line SQL dump containing several databases worth of data. 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.