我目前正在我的一个客户端SQL-Server环境中工作。一年前,我被要求在我正在制定的维护计划中实施.csv报告生成。为此,我在服务器上启用了xp_cmdshell选项并使用了bcp实用程序。但是,来自另一家公司的程序员最近添加了一个存储过程,在该过程中,他在过程开始时启用了xp_cmdshell选项,并在其结束时禁用它(设置为0)。此功能导致我的维护计划在报告生成步骤中失败。
由于我不允许更改他的代码,因此我被要求在我的脚本中包含类似的启用/禁用功能。以下是我找到并使用的代码示例:
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;
问题是我对此解决方案的安全性并不完全确定,但我不想重新设计我编写的代码。我也在其他一些脚本中使用xp_cmdshell功能。
问题是:在同时执行脚本B(在存储过程中使用)中禁用xp_cmdshell时,是否可能会影响脚本A(由我的维护计划使用)的执行?
我已经对SQL-Server查询处理器做了一些研究,以了解它是如何工作的并执行查询但是找不到这个问题的答案。
我将非常感谢您的任何建议!