Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 RESOLVED - SQL - Select records, loop, INSERT...

Author  Topic 

mattboy_slim
Yak Posting Veteran

72 Posts

Posted - 2009-02-27 : 17:36:25
OK, since I am VERY (very) new to T-SQL, but familiar with ASP, I have a conundrum, but hopefully a basic understanding of how this should work. I need to find records where machines have not reported in, in the last 15 minutes. Then, insert records into an 'alerts' table IF (and only if) no alert has previously been written for that machine. My sample (and comment) is below (forgive me for completely incorrect and incomplete syntax):



Sample:

/* select machines where the time it last reported was more than 15 minutes ago */
SELECT f_machineID, f_lastreported from tb_machines where f_lastreported < DateAdd("n",-15,now())

/* start looping through the recordset */
FOR EACH ITEM IN recordset

/* select machines that match the current record */
SELECT f_machineID from tb_machines where f_machineID = currentrecord("f_machineID")

/* if machine does not have an alert already, then create an alert */
if recordset is empty then

INSERT INTO tb_alerts (f_alertname, f_alertgenerated, f_machineID) VALUES ('Machine Not Reporting', now(), currentrecord("f_machineID"));

end if

LOOP

I’ve been researching this for a few hours with not much help (of course, it would help if I understood the language better, but I’m working on that). The following page seems to have some good examples with INSERT INTO with sub-queries, but I’m not sure if this is what I need. If this is possible with the examples on the page below sample here:
INSERT INTO Residents (Name, Occupation)
SELECT Name, Occupation FROM Immigration
WHERE Residency = 'granted';

http://www.devguru.com/technologies/t-sql/7124.asp

Thanks in advance for any help you can offer,
Matt

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-02-27 : 18:18:33
hey slim
I believe this is how you do it...
INSERT INTO tb_alerts (f_alertname, f_machineID,f_alertgenerated)
SELECT f_alertname,
f_machineID,
f_lastreported
FROM tb_machines
WHERE f_lastreported < DATEADD(minute , DATEDIFF(minute , 0, getdate())-15, 0)

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-27 : 18:25:55
[code]INSERT tb_alerts
(
f_alertname,
f_alertgenerated,
f_machineID
)
SELECT 'Machine Not Reporting',
GETDATE(),
x.f_machineID
FROM tb_machines AS x
WHERE x.f_lastreported < DATEADD(MINUTE, -15, GETDATE())
AND NOT EXISTS (SELECT * FROM tb_alerts AS y WHERE y.f_machineID = x.f_machineID)[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

mattboy_slim
Yak Posting Veteran

72 Posts

Posted - 2009-03-02 : 09:19:47
Thanks guys, since I posted this on Friday at the end of the day I'm just now getting back to it on Monday. I will test these and report back!

Matt
Go to Top of Page

mattboy_slim
Yak Posting Veteran

72 Posts

Posted - 2009-03-02 : 17:18:40
yosiasz, your code works great with one caveat: I need it to only write records more machines that previously don't have records.

Peso, yours works flawlessly. I've had it running every (5) minutes on the SQL server, and it is working exactly as intended.

Thanks to both of you for taking the time to reply. This forum is spectacular!

Matt
Go to Top of Page
   

- Advertisement -