SET @cmd value based on select name

  • Hello,

    I am trying to move files to directories based on the file status. If the file contains the Flag='E' then I want to move it to an "Error" folder, otherwise it will go to a "Processed" folder.

    Here is my current code, can someone help me understand what I am doing incorrectly?

    DECLARE @cmd varchar(500)

    SELECT

    CASE

    WHEN Processed_Flag = 'E'

    THEN

    SET @cmd='MOVE /Y C:\temp\myfiles\*.dat C:\temp\myfiles\Error'

    ELSE

    SET @cmd='MOVE /Y C:\temp\myfiles\*.dat C:\temp\myfiles\Processed'

    END

    FROM#TEMP

    PRINT @Cmd

  • Your command doesn't specify which file to move. It just moves all .dat files either to the Error folder or the Processed folder. Of course, once you've done that the first time, there'll be nothing to move the second time.

    John

  • Thank you John! Amending my original code, I would actually like to do something like this if possible:

    DECLARE @cmd varchar(500)

    SELECT filename,

    CASE

    WHEN Processed_Flag = 'E'

    THEN

    SET @cmd='MOVE /Y C:\temp\myfiles\' + filename + '.dat C:\temp\myfiles\Error'

    ELSE

    SET @cmd='MOVE /Y C:\temp\myfiles\' + filename + '.dat C:\temp\myfiles\Processed'

    END

    FROM #TEMP

    PRINT @Cmd

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply