In cases you will have to implement from a development to a user environment and do not want to run the scrip on the wrong server environment by using the below script the system will only run on the server you specified. the only draw back is that you cannot use the GO in the script then. but it is pretty useful to ensure you don't run on the wrong environment.
DECLARE @RightServerRun VARCHAR(20) = 'local host'
/***************************************************************************************************************************
CHECKS IF THE SCRIPT IS ON THE RIGHT SERVER, IF SO IT EXECUTES THE WHILE BEGIN...
***************************************************************************************************************************/
IF (SELECT ServerCheck.srvname FROM master.dbo.sysservers ServerCheck WHERE ServerCheck.srvid = 0) != @RightServerRun
PRINT 'This Script is not running on the Right Server change to ' + @RightServerRun
ELSE
WHILE (SELECT ServerCheck.srvname FROM master.dbo.sysservers ServerCheck WHERE ServerCheck.srvid = 0) = @RightServerRun
BEGIN
IF (SELECT ServerCheck.srvname FROM master.dbo.sysservers ServerCheck WHERE ServerCheck.srvid = 0) != @RightServerRun
BREAK
PRINT '------------------------------------------------------------------------------------------------------------';
PRINT 'START:';
/***************************************************************************************************************************
START THE SCRIP TO IMPLEMENT BELOW:
***************************************************************************************************************************/
/***************************************************************************************************************************
ENDS THE WHILE STATEMENT AND RUN
***************************************************************************************************************************/
PRINT '------------------------------------------------------------------------------------------------------------';
PRINT 'END of Script';
IF (SELECT ServerCheck.srvname FROM master.dbo.sysservers ServerCheck WHERE ServerCheck.srvid = 0) = @RightServerRun
SET @RightServerRun = 'END'
END
if you run the SQL statement on the wrong server you will get the following results:
This Script is not running on the Right Server change to local host
Else if it is the right server the results will be as follows:
------------------------------------------------------------------------------------------------------------
START:
------------------------------------------------------------------------------------------------------------
END of Script
No comments:
Post a Comment