Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Execute a select statement continually - for instance every 5 minutes


Execute a select statement continually - for instance every 5 minutes

Author
Message
rarara
rarara
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 419
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??
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21113 Visits: 18259
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

rarara
rarara
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 419
Unfortunately I live in the world of Change Management Angry 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.
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21113 Visits: 18259
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

rarara
rarara
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 419
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16639 Visits: 17026
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search