Solved

how to write logic in trigger if(TOTAL_LARGE > CURRENT_LARGE) then rollback the transaction.

Posted on 2013-11-12
1
299 Views
Last Modified: 2013-11-17
CREATE TRIGGER [dbo].[EMPJOBS_Trigger] ON [EMPjobs]
FOR INSERT
AS
BEGIN

select location_jobs.id as job_id
      ,     location_jobs.pool_large as TOTAL_LARGE
      ,     sum( case when ( job.job_type='LARGE' and job.job_status='NEW') then 1 else 0 end) as CURRENT_LARGE
    from locations location_jobs    
         LEFT OUTER JOIN emp_jobs job  on location_jobs.id=job.storage_id
              and job.storage_id=(SELECT  storageid  from INSERTED) and job.job_type in ('LARGE')
              and job.job_status='NEW'
    where location_jobs.id=(SELECT  storageid  from INSERTED)
    group by location_jobs.id, location_jobs.pool_large
      
      how to write logic in trigger if(TOTAL_LARGE > CURRENT_LARGE) then rollback the transaction.
      
      if(TOTAL_LARGE > CURRENT_LARGE)
  BEGIN
    PRINT 'exceeded the limit.'
    RAISERROR ('exceeded the limit. ')
    ROLLBACK TRANSACTION
    RETURN
  END
 
  END
  GO
0
Comment
Question by:chaitu chaitu
1 Comment
 
LVL 15

Accepted Solution

by:
dbbishop earned 500 total points
ID: 39646273
Will EMPjobs ever have more than one row inserted at a time? Before you answer, think of all scenarios where it could, a batch process, maintenance, etc. If it can, you cannot use the logic flow you have above.
"job.storage_id=(SELECT  storageid  from INSERTED)" must return one, and only one row. Same with the subsequent WHERE clause. If more than one row exists in inserted, an error will be generated.

All that aside, assuming only one row will ever be inserted at a time, try something like this:
CREATE TRIGGER [dbo].[EMPJOBS_Trigger] ON [EMPjobs]
	FOR INSERT
AS
	BEGIN

		DECLARE	@TotalLarge INT
		DECLARE	@CurrentLarge INT 

		SELECT	@TotalLarge = location_jobs.pool_large,
				@CurrentLarge = SUM(CASE WHEN job.job_type = 'LARGE'
											  AND job.job_status = 'NEW'
										 THEN 1
										 ELSE 0
									END)
		FROM	locations.location_jobs
		LEFT OUTER JOIN emp_jobs job
		ON		location_jobs.id = job.storage_id
				AND job.storage_id = ( SELECT storageid FROM INSERTED )
				AND job.job_type IN ( 'LARGE' )
				AND job.job_status = 'NEW'
		WHERE	location_jobs.id = ( SELECT storageid FROM INSERTED )
		GROUP BY location_jobs.id,
				location_jobs.pool_large
      
--      how to write logic in trigger if(TOTAL_LARGE > CURRENT_LARGE) then rollback the transaction.
      
		IF @TotalLarge > @CurrentLarge 
			BEGIN
				RAISERROR ('Exceeded the limit.', 0, 1)
				ROLLBACK TRANSACTION
				RETURN
			END
  
	END
  GO 

Open in new window

0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question