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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:11:02+00:00 2026-05-24T09:11:02+00:00

In SQL Server Management Studio, I repeatedly manage to run sql scripts against the

  • 0

In SQL Server Management Studio, I repeatedly manage to run sql scripts against the master database rather the specific database that I intend. I open a SQL file and then click run, repeatedly forgetting to set the database first.

I know you can set a default for a server connection – but then I might end up running sql on the default database rather than my intended one if my intended one is not the default.

USE statements in the sql won’t work either – the scripts need to be run against multiple different test databases.

So is there anyway to flag a warning that the script is about to be run against the master database? Or possibly force the selection of a database first before a script is run?

  • 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-24T09:11:03+00:00Added an answer on May 24, 2026 at 9:11 am

    One way is to connect with an account that has permissions only in the required databases. Not sysadmin which is what you are doing now.

    This would be normal for developers

    If you want to run as sysadmin, add this line to each script

    IF DB_NAME() = 'master'
        RAISERROR ('Oi! Stop!', 20, 1)  WITH LOG
    GO
    

    This will kill the connection

    Edit:

    I like Quassnoi’s solution because it doesn’t require sysadmin rights.
    But, you can’t give feedback or revert.

    So, I’ve been playing with this. It checks most boxes

    • amusing message
    • no sysadmin needed
    • re-runnable (kind of)

    There is one issue that an SSMS USE (via the drop down) is ignored (as per Quassnoi’s notes), but it’s getting there. Any more ideas anyone?

    SET NOEXEC OFF;
    GO
    DECLARE @isOK bit;
    BEGIN TRY
        IF DB_NAME() IN ('master', 'protectedDB1', '...')
           RAISERROR ('To Catch Block Only', 16, 1);
        SET @isOK = 1;
    END TRY
    BEGIN CATCH
        SET @isOK = 0;
        RAISERROR ('Oi! Stop!', 16, 1);
    END CATCH
    IF @isOK = 0
        SET NOEXEC ON;
    GO
    SELECT 1;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When executing scripts in SQL Server Management Studio, messages are often generated that display
Using MS SQL Server Management Studio 2005 - To Restore a Database: Restore Database
In Microsoft SQL Server Management Studio 2008 there is a Generate scripts... option under
I'm using Microsoft SQL Server Management Studio to work with a database. I'm only
When I run any query in sql server management studio, I get the following
In SQL Server Management Studio, there is an option to set the default database
In SQL Server Management Studio, I want to execute a number of SQL scripts
In SQL Server Management Studio, if I attempt to restore a database from a
TITLE: Microsoft SQL Server Management Studio Express Database diagram support objects cannot be installed
I am using SQL Server Management Studio to run a script on SQL Server

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.