Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Execute a select statement continually - for instance every 5 minutes Expand / Collapse
Author
Message
Posted Tuesday, April 22, 2014 7:51 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:52 AM
Points: 32, Visits: 343
This is probably easy, but I can't find it mentioned....

Anyway, I need to connect to our SQL estate via a remote SQLSMS connection which seems to have a shortish time-out that is irritating, as its a lengthy process signing-in again.

I have a select script that I like to check through the day. Is there any way to avoid the time-outs by running this script repeatedly, at say a 5 minute interval to display the results on screen? Even better would be if I could also specify a finish time so that it will say not execute beyond 5pm.

Sound like it should be do-able, right - begin/wait/loop etc??
Post #1563860
Posted Tuesday, April 22, 2014 7:54 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 3:27 PM
Points: 21,631, Visits: 15,289
easy enough to put it into a sql agent job and then have the select insert into a table. then you can just query that table whenever you would like.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1563862
Posted Tuesday, April 22, 2014 7:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:52 AM
Points: 32, Visits: 343
Unfortunately I live in the world of Change Management where even a fart needs the correct approval, testing and lead-time. Adding new jobs willy-nilly isn't possible.

This all needs to be done within the syntax of T-SQL and in a query window.
Post #1563865
Posted Tuesday, April 22, 2014 8:05 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 3:27 PM
Points: 21,631, Visits: 15,289
you can loop it.

or you could do the agent job on a local instance of sql server(if u have one) and then run the select across a linked server).

simple example

DECLARE @i INT = 1;

WHILE (@i <= 60)
BEGIN
WAITFOR DELAY '00:00:01'

/*Your Script*/

SET @i = @i + 1;
END





Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1563868
Posted Tuesday, April 22, 2014 8:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 3:52 AM
Points: 32, Visits: 343
Found this which seems to work:

SELECT GETDATE() --your query to run
raiserror('',0,1) with nowait --to flush the buffer
waitfor delay '00:00:10' --pause for 10 seconds
GO 5 --loop 5 times


With the only issues being-

1) It appends to the results window - would rather it overwrote

2) No end-time, though suspect that could easily be done, or I could use simple arithmetic to run for a fixed period.
Post #1563870
Posted Tuesday, April 22, 2014 8:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
If you use the looping method posted you could change the condition of the where to use some date math so it will stop running by 5pm as you requested.

while GETDATE() < dateadd(hour, 17, dateadd(dd, datediff(dd, 0, GETDATE()), 0))



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1563872
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse