Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL中使用xp_cmdshell检查现有文件,并在cmdshell中返回一个值_Sql_Sql Server_Stored Procedures_Xp Cmdshell - Fatal编程技术网

在SQL中使用xp_cmdshell检查现有文件,并在cmdshell中返回一个值

在SQL中使用xp_cmdshell检查现有文件,并在cmdshell中返回一个值,sql,sql-server,stored-procedures,xp-cmdshell,Sql,Sql Server,Stored Procedures,Xp Cmdshell,我正在尝试使用SP-MSSQL创建一个文件。在创建文件之前,我想检查文件是否存在。如果存在,我想替换一个新文件或创建一个新文件。希望在sql中执行cmdshell时返回值 DECLARE @Text AS VARCHAR(100) DECLARE @Cmd AS VARCHAR(100) DECLARE @exstFile AS VARCHAR(100) set @cmd = 'I

我正在尝试使用SP-MSSQL创建一个文件。在创建文件之前,我想检查文件是否存在。如果存在,我想替换一个新文件或创建一个新文件。希望在sql中执行cmdshell时返回值

               DECLARE @Text AS VARCHAR(100)
               DECLARE @Cmd AS VARCHAR(100)
               DECLARE @exstFile AS VARCHAR(100)

            set @cmd = 'IF EXIST Z:\AppTextFile.txt (ECHO 1) ELSE (ECHO 0)'
            EXEC @cmd = xp_cmdshell @cmd
            select @cmd datum

检查文件是否存在的另一种方法可以是:


不要使用
ECHO
,请尝试
EXIT
设置退出代码。为什么不使用
xp\u fileexist
我记得此SP仅用于UNC共享名,不用于网络驱动器。现在修好了吗?谢谢兄弟。。。非常感谢你。这对我很有效@阿里先生
DECLARE @Exists INT;

EXEC master..xp_FileExist N'U:\SSIS\Test.txt', @Exists OUTPUT

select @Exists