Solved

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

Posted on 2013-11-12
1
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Split string into 3 separate fields 5 36
Display SQL maintenance plan SQL Code 3 47
T-SQL: Wrong Result 7 39
Output Different in Excel Compared In SSMS 8 14
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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