-1

I am trying to create a file using the SP - MSSQL. before creating a file i want to check Whether the file exist . IF Exist i want to replace a new one or else create a new file. Want to return a value while executing the cmdshell in sql.

               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
Manu C Rajan
  • 153
  • 1
  • 2
  • 13

1 Answers1

0

An alternative way for to check files existence could be to use xp_FileExist:

DECLARE @Exists INT;

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

select @Exists
M.Ali
  • 67,945
  • 13
  • 101
  • 127