I am currently working at one of my clients SQL-Server environment. A year ago I was asked to implement a .csv report generation in a Maintenance Plan I was working on. To do so, I have enabled xp_cmdshell option on the server and used bcp utility. However, a programmer from the other company recently added a stored procedure in which he enables xp_cmdshell option at the beginning of procedure and disables it (sets to 0) at its end. This feature have caused my Maintenance Plan to fail at the report generation step.
Since I'm not allowed to change his code I was asked to include similar enabling/disabling feature in my scripts. Here is example of code I found and used:
declare @prevAdvancedOptions int;
declare @prevXpCmdshell int;
select @prevAdvancedOptions = cast(value_in_use as int) from sys.configurations where name = 'show advanced options';
select @prevXpCmdshell = cast(value_in_use as int) from sys.configurations where name = 'xp_cmdshell';
if (@prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 1;
reconfigure;
end;
if (@prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 1;
reconfigure;
end;
--- doing some work here ---
if (@prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 0;
reconfigure;
end;
if (@prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 0;
reconfigure;
end;
The thing is I'm not completly sure about the safety of this solution but I don't want to redesign the code I wrote. I am also using xp_cmdshell feature in some other scripts.
The question is: Is it possible that execution of Script A (used by my maintenance plan) can be affected with disabling xp_cmdshell in Script B (used in the stored procedure) when they are executed at the same time?
I have done some research about SQL-Server Query Processor to understand how it works and executes the queries but could not find an answer to this question.
I will appreciate any of your suggestions!