• donne4real (1/6/2010)


    Here is my code but for some reason, it seems not to be working correctly. It is not going through the loop as expected and will appreciate it if you guys can help me with it. I added some print statements within the code to be able to see where it is getting stuck and it seems to be within the loop where the issue is.

    BEGIN

    declare @Cmd varchar(255)

    declare @filename varchar(255)

    declare @expectedfilename varchar(255)

    declare @batfilename varchar(255)

    declare @sys_error int

    declare @outputcount int

    declare DailyMail_Cursor cursor local fast_forward

    for

    select filename from dailymail

    open DailyMail_Cursor

    fetch next from DailyMail_Cursor into @filename

    while @@fetch_status = 0

    begin

    select @expectedfilename = expectedfilename from FILEDIRECTORY

    select @batfilename = batfilename from FILEDIRECTORY where @expectedfilename =@filename

    print 'test'

    select @outputcount = count(*) from filedirectory where @expectedfilename =@filename

    print @outputcount

    if @outputcount <>0

    --if @expectedfilename = @filename

    print 'after if stmt'

    begin

    print 'after the inner begin stmt'

    select @Cmd = 'C:\test\' + @batfilename

    print @cmd

    EXECUTE @sys_error = master.dbo.xp_cmdshell @cmd--, no_output

    end

    fetch next from DailyMail_Cursor into @filename

    end

    print 'after begin'

    --

    END

    close DailyMail_Cursor

    deallocate DailyMail_Cursor

    I recommend changing this to a set based method.

    You are missing a ' at the end of your select @Cmd statement.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events