Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-12
1
Medium Priority
?
314 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 2000 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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