Cursor stop query at specific time

  • This store procedure will get some executable queries from the select statement, the cursor will fetch each rows to execute the query and insert the queries into table_3 to mark as 'E'. Until 17:00, this store procedure will stop execute the queries and just get the queries from select statement insert into table_3 to mark as 'C'.

    I don't know why the outputs in table_3 are quiet different than I think. This store procedure comes out with two exactly same queries and one marked as C and another marked as E.

    CREATE PROCEDURE procedure1

    AS

    DECLARE cursor_1 CURSOR FOR

    SELECT

    'This is a executable query'

    FROM table_1

    DECLARE @table_2

    DECLARE @stoptime DATETIME = NULL;

    SET @stoptime = CONVERT( CHAR(8), GetDate(), 14);

    OPEN cursor_1

    FETCH NEXT FROM cursor_1 INTO @table_2;

    WHILE (@@FETCH_STATUS = 0)

    IF (@stoptime < '17:00:00')

    BEGIN

    EXEC sp_executesql @table_2

    INSERT INTO table_3 VALUES (@table_2,'C')

    FETCH NEXT FROM cursor_1 INTO @table_2

    END

    ELSE

    BEGIN

    INSERT INTO table_3 VALUES (@table_2,'E');

    END

    FETCH NEXT FROM cursor_1 INTO @table_2

    CLOSE cursor_1;

    DEALLOCATE cursor_1;

  • Please don't cross post questions, it fragments replies.

    http://www.sqlservercentral.com/Forums/Topic1732107-3411-1.aspx

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

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