Solved

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

Posted on 2013-11-12
1
286 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now