Solved

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

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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