set System Date with cmdshell

  • I have created SP like this

    CREATE PROC CancellationTestCase

     @StartDate as varchar(10), @DaysToRun as int --repeat until x Days

    AS

    BEGIN

    DECLARE @Today DATETIME,

      @intLoop INT,

      @SetDateCommand varchar(20),

      @dateLoop DATETIME,

      @dateTest DATETIME

    --Save CurrentDate

    SET @TODAY = GETDATE()

    --Action Loop

    SET @intLoop = 0

    WHILE @intloop < @DaysToRun

     BEGIN

     --change SytemDate = @StartDate

     set @dateLoop = dateadd(dd,@intLoop,convert(DATETIME,@startdate))

     Set @SetDateCommand = 'date ' + convert(varchar,@dateLoop,110)

     Execute master.dbo.xp_cmdshell @SetDateCommand 

     SET @dateTest = GETDATE()

     select @dateTest,GETDATE() 

     SET @intLoop = @intLoop + 1 

    END

    --Restore Date

    SET @setdatecommand = 'date ' + convert(varchar,@today,110)

    EXEC master.dbo.xp_cmdshell @setdatecommand

    END

    when i debug (SP debug in Query Analyser), @dateTest have a correct value (incremented), but when I Execute ("execute CancellationTestCase @startdate='04-20-2007',@DaysToRun=7"), Getdate is still today's date,@datetest  is not incremented. Anybody know what's wrong ?

  • hi ,

    just put a WAITFOR DELAY '00:00:01' statement after

    SET @dateTest = GETDATE() statement

    Set @SetDateCommand = 'date ' + convert(varchar,@dateLoop,110)

     Execute master.dbo.xp_cmdshell @SetDateCommand 

     SET @dateTest = GETDATE()

     select @dateTest,GETDATE() 

    WAITFOR DELAY '00:00:01'

     SET @intLoop = @intLoop + 1 

    and it works

  • Thanks... it works..

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

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