Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Administration - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 6525

Cursor stop query at specific time

$
0
0
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.[quote]CREATE PROCEDURE procedure1ASDECLARE cursor_1 CURSOR FOR SELECT 'This is a executable query' FROM table_1DECLARE @table_2DECLARE @stoptime DATETIME = NULL;SET @stoptime = CONVERT( CHAR(8), GetDate(), 14);OPEN cursor_1FETCH 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; [/quote]

Viewing all articles
Browse latest Browse all 6525

Trending Articles