sql exec copy file failed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    sql exec copy file failed

    I need help to make this procedure copying file works for me.

    The error is
    Msg 50000, Level 16, State 1, Procedure sp_copyfile, Line 23
    File copy failed

    Code:
    USE [NGProd]
    GO
    /****** Object:  StoredProcedure [dbo].[copyfile]    Script Date: 12/20/2011 10:37:44 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    ALTER PROCEDURE [dbo].[copyfile] @sourcefilepath sysname, @targetfilepath sysname=NULL
    AS
    
    SET NOCOUNT ON
    IF (@sourcefilepath='/?') OR (@targetfilepath IS NULL) GOTO Help
    
    DECLARE @cmdstr varchar(8000)
    
    CREATE TABLE #cmd_result (output varchar(8000))
    
    EXEC master..xp_sprintf @cmdstr OUTPUT, 'copy %s %s',@sourcefilepath, @targetfilepath
    
    INSERT #cmd_result
    EXEC master..xp_cmdshell @cmdstr
    
    SELECT * FROM #cmd_result
    
    IF EXISTS(SELECT * FROM #cmd_result WHERE output like '%file(s) copied%') BEGIN
        SET @cmdstr='The file copy operation "'+@cmdstr+'" was successful (at least one file was copied)'
        PRINT @cmdstr
        EXEC master..xp_logevent 60000, @cmdstr, 'INFORMATIONAL'
    END ELSE RAISERROR('File copy failed',16,1)
    
    DROP TABLE #cmd_result 
    RETURN 0
    
    Help:
    
    print 'Error'
Working...