August 20, 2014 at 10:24 am
Hi,
I would like to force this script to look at the Dev Server only to avoid running it accidentally from production if someone doesn't pay attention which server they are connected on and cause some major damage. Below is a simple example I've put together, obviously the real script is updating records ,dropping tables etc. which we don't want to see on happening on Production 😉
USE MyTestDB
Select * From MyTable
So instead of the above I'd like something like this so it's locked down to my Dev01 Server:
USE [Dev01\MSSQLSERVER].MyTestDB
Select * From MyTable
I'm not having any luck with the above syntax, is this even doable?
Thanks
August 20, 2014 at 10:41 am
One thought is to look at @@SERVERNAME. Maybe you could raise an error or something if it doesn't match the name of the dev box?
August 20, 2014 at 10:49 am
Why don't you remove permissions to developers on production?
August 20, 2014 at 10:57 am
Jim has one idea.
Another way to do it is to script requiring SQLCMD mode, then you can specify the connection in the script. You can include this snippet is borrowed from an SSDT database project publish script:
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
Which will keep the script from running if SQLCMD mode isn't on and you can also then check @@SERVERNAME and do the same thing the previous snippet does to stop execution.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 20, 2014 at 11:10 am
Jim Arko (8/20/2014)
One thought is to look at @@SERVERNAME. Maybe you could raise an error or something if it doesn't match the name of the dev box?
Jim,
What I have in my script is the actual server name and instance name I got from @@ServerName and @@ServiceName.
The error that I'm getting is :
"Database 'dev01\mssqlserver' does not exist. Make sure that the name is entered correctly"
Obviously the above is not my database name so it's not parsing it properly. Any thoughts?
August 20, 2014 at 11:12 am
Jack Corbett (8/20/2014)
Jim has one idea.Another way to do it is to script requiring SQLCMD mode, then you can specify the connection in the script. You can include this snippet is borrowed from an SSDT database project publish script:
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
Which will keep the script from running if SQLCMD mode isn't on and you can also then check @@SERVERNAME and do the same thing the previous snippet does to stop execution.
I'll give this a try thanks. I will update the post once I make some progress.
August 20, 2014 at 11:22 am
SQLnbe (8/20/2014)
Jim Arko (8/20/2014)
One thought is to look at @@SERVERNAME. Maybe you could raise an error or something if it doesn't match the name of the dev box?Jim,
What I have in my script is the actual server name and instance name I got from @@ServerName and @@ServiceName.
The error that I'm getting is :
"Database 'dev01\mssqlserver' does not exist. Make sure that the name is entered correctly"
Obviously the above is not my database name so it's not parsing it properly. Any thoughts?
I apologize for not clarifying, your syntax won't work as the valid syntax for USE (from BOL) is USE { database } so you won't be able to specify the instance there. My idea was to add logic that looks at the @@SERVERNAME value and raises an error or otherwise prevents execution if that returns anything but the name of your dev server.
August 22, 2014 at 7:09 am
August 22, 2014 at 7:53 am
Sean Pearce (8/22/2014)
Using RAISERROR with a severity level of 20 will disconnect the session immediately.
IF (@@SERVERNAME='Prod')
BEGIN
RAISERROR('Incorrect Server', 20, -1) WITH LOG;
END;
GO
Thanks
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply