使用SQL Server过程打开批处理文件

时间:2013-06-18 12:37:40

标签: sql sql-server

使用SQL Server程序打开批处理文件....

为此,我举了打开记事本的例子。并且能够使用以下代码打开它:

start "c:\windows\system32" notepad.exe
--- saved as : note.bat

我尝试按照以下程序打开它:

ALTER procedure [dbo].[executebat]
as
begin
   EXEC master.dbo.xp_CMDShell 'D:\ducont\note.bat'
end

我还使用以下代码::

启用了xp_cmdshell选项
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

但是当我尝试执行该程序时,它会不断显示

" Query Executing..."

没有输出!!

如果我在任何地方出错,请指导我。

1 个答案:

答案 0 :(得分:0)

简单的回答。当您运行xp_cmdshell时,它会创建一个shell来执行您要求的任何工作。您的bat文件在该shell中运行,并将在shell中再次打开记事本。你将无法看到它。由于它期望输入(并且无法获得任何输入),因此您会遇到“查询执行...”

除了可能使用CLR之外,确实没有其他方法可以做你要求的事情。不幸的是,我不知道该怎么说某种方式。在SQL 2012或2014中可能有一种更简单的方法,但我还没有看到它。