Click here to Skip to main content
15,887,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my SQL trigger code. trigger will insert the record from table1 to table3 for active services based on tolerancetime. table3 is getting cleared in certain polling interval.in table3 insertion (from trigger) and deletion (polling interval) happens

for example if active service count is 3 , but the record inserted for only two services.

SQL
ALTER TRIGGER [dbo].[insTrigger]
ON [dbo].[table1]
AFTER INSERT
AS
BEGIN
	
	DECLARE @servicecount INT   
	DECLARE @count INT	  
	DECLARE @tolerancetime INT	
	
	
	SET @tolerancetime =60
	SET @servicecount = (SELECT count(1) FROM [dbo].[table2] WITH (NOLOCK) WHERE servicetime >= DATEADD(mi,-@tolerancetime,GETDATE())) 
  
	
  IF(@servicecount != 0)

   BEGIN	 

	-- Looping till active service count
      WHILE(@count <= @servicecount)
	   BEGIN
	
	INSERT INTO table3 
			SELECT logid
			servicetype,			
			(SELECT rn.servicename FROM 
			(SELECT  row_number() OVER(ORDER BY t2.servicename ASC) AS rowno,t2.servicename,t2.servicetime FROM table2 t2 
			 WHERE t2.servicetime >= DATEADD(mi,-@tolerancetime,GETDATE())) rn
			 WHERE rn.rowno=@count)			 
			FROM inserted  
		WHERE 
			servicetype = started
	  	    AND 
			servicestarttime >= (SELECT t2.servicetime FROM(SELECT  row_number() OVER(ORDER BY t2.servicename ASC) AS rowno,t2.servicetime FROM table2 t2 
				WHERE t2.servicetime >= DATEADD(mi,-@tolerancetime,GETDATE())) rn
				WHERE rn.rowno = @count)
			
		SET @count = @count + 1
								
	   END
   END
END



please suggest any idea to resolve this issue to insert record for all services
Posted
Updated 19-Jan-16 21:10pm
v3
Comments
dan!sh 20-Jan-16 2:45am    
Looks like you have multiple pre tags in the question. Can you edit the question to format the SQL code properly?
vksvpp 20-Jan-16 7:11am    
ALTER TRIGGER [dbo].[insTrigger]
ON [dbo].[table1]
AFTER INSERT
AS
BEGIN

DECLARE @servicecount INT
DECLARE @count INT
DECLARE @tolerancetime INT


SET @tolerancetime =60
SET @servicecount = (SELECT count(1) FROM [dbo].[table2] WITH (NOLOCK) WHERE servicetime >= DATEADD(mi,-@tolerancetime,GETDATE()))


IF(@servicecount != 0)

BEGIN

-- Looping till active service count
WHILE(@count <= @servicecount)
BEGIN

INSERT INTO table3
SELECT logid
servicetype,
(SELECT rn.servicename FROM
(SELECT row_number() OVER(ORDER BY t2.servicename ASC) AS rowno,t2.servicename,t2.servicetime FROM table2 t2
WHERE t2.servicetime >= DATEADD(mi,-@tolerancetime,GETDATE())) rn
WHERE rn.rowno=@count)
FROM inserted
WHERE
servicetype = started
AND
servicestarttime >= (SELECT t2.servicetime FROM(SELECT row_number() OVER(ORDER BY t2.servicename ASC) AS rowno,t2.servicetime FROM table2 t2
WHERE t2.servicetime >= DATEADD(mi,-@tolerancetime,GETDATE())) rn
WHERE rn.rowno = @count)

SET @count = @count + 1

END
END
END
_Asif_ 20-Jan-16 3:12am    
What is the actual error you are getting? You have not mentioned the problem you are getting while inserting?
vksvpp 20-Jan-16 6:56am    
if i am checking table2, it shows 3 services are active. sometimes the record is not inserted in any one of the active service(table3). if polling and insertion happen at sametime in table3 whether the trigger record should be inserted or not ?
Corporal Agarn 20-Jan-16 14:33pm    
The way I read it you are going to be adding a lot more records than you think.
How is inserted related to table2? You also need to list the insert columns (best practice).

I came up with something like this:
INSERT INTO table3 (logid, servicetype, servicename)
SELECT ROW_NUMBER() OVER(ORDER BY t2.servicename ASC) AS rowno,
t2.servicename, t2.servicetime
FROM inserted i
INNER JOIN table2 t2 ON i.??? = t2.???
WHERE t2.servicetime >= DATEADD(mi,-@tolerancetime,GETDATE())

1 solution

ROW_NUMBER (Transact-SQL)[^] starts from 1 but your loop starts from 0... so there is no row 0, it inserts rows 1 and 2 and never gets to insert row 3.

Initialise count to 1
 
Share this answer
 
Comments
vksvpp 20-Jan-16 6:58am    
here i forgot to set @count as 1.but in my code i have set it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900