Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check database name before running SQL

Tags:

sql

sql-server

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?

like image 813
icc97 Avatar asked Aug 05 '11 09:08

icc97


People also ask

How do I find my database server name?

In Microsoft SQL Server Management Studio, in the Object Explorer pane, right click the server and select properties. In the pane, there should be a heading called "Connection" and in that heading a link to a new window called "View connection properties". The value next to "Server name" is the name of your server.

How can I tell if a SQL database was created?

In creating a database you also need to check whether or not the database already exists. In order to do so, simply use the 'if exists' method and select the name of the database from sysdatabases.


2 Answers

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
like image 57
gbn Avatar answered Oct 04 '22 04:10

gbn


IF DB_NAME() = 'master'
        SET NOEXEC ON
GO
SELECT  1
GO

SET NOEXEC will make the rest of the statements in the script compiled but not executed.

You will have to issue SET NOEXEC OFF before you will be able to execute anything in this connection (even select the database in the drop down box).

Alternatively just close and reopen the script file, it will close and reopen the connection.

like image 36
Quassnoi Avatar answered Oct 04 '22 04:10

Quassnoi