Solved

trigger problem using inserted table on insert or update

Posted on 2006-07-19
5
585 Views
Last Modified: 2010-08-05
Hi,

Here is the code to create the table
---begin creation code
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblPassRange]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tblPassRange]
GO

CREATE TABLE [dbo].[tblPassRange] (
      [TicketTypeID] [int] NOT NULL ,
      [PassRangeFirst] [int] NULL ,
      [PassRangeLast] [int] NULL
) ON [PRIMARY]
GO
--end creation code

below is the insert trigger, followed by the update trigger ->
--begin insert trigger
CREATE TRIGGER T_tblPassRange_ITrig ON dbo.tblPassRange FOR insert AS

if (SELECT COUNT(*) FROM inserted INNER JOIN tblPassRange ON inserted.TicketTypeID = tblPassRange.TicketTypeID WHERE (inserted.PassRangeFirst >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeFirst <= tblPassRange.PassRangeLast) OR (inserted.PassRangeLast >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeLast <= tblPassRange.PassRangeLast)) > 0
      begin
            RAISERROR 44444 'The TicketType range you are requesting is already allocated '
            rollback transaction
        end            
--end insert trigger

--begin Update trigger
CREATE TRIGGER T_tblPR_UTrig ON dbo.tblPassRange FOR update AS

if (SELECT COUNT(*) FROM inserted INNER JOIN tblPassRange ON inserted.TicketTypeID = tblPassRange.TicketTypeID WHERE (inserted.PassRangeFirst >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeFirst <= tblPassRange.PassRangeLast) OR (inserted.PassRangeLast >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeLast <= tblPassRange.PassRangeLast)) > 0
      begin
            RAISERROR 44444 'The TicketType range you are requesting is already allocated '
            rollback transaction
        end            
--end update trigger

The idea is to stop a ticket range for passes in our system from allocating the same pass numbers twice for a given ticket type, eg for ticket type 1, you cannot have the Passrange going from 1-3 on one record, and 2-4 on another.
However, what is happenning is that

(a) on the insert, no tickets can be allocated where anything is put in PassRangeFirst or PassRangeLast, it always rolls back no matter what I put in on the same ticket type, unless I let PassRangeFirst and PassRangeLast be null

(b) on the update trigger, it's saying "Another user has modified the contents of this table or view; the database row you are modifying no longer exists in the database", then generates the rollback.

Spent half a day trying to figure out where I have fudged it, hope someone can clue me in.

thx,
Shaun
0
Comment
Question by:raswa
5 Comments
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 17136690
So clearly this statement is always returning > 0:

SELECT COUNT(*)
FROM
inserted
INNER JOIN
tblPassRange
ON inserted.TicketTypeID = tblPassRange.TicketTypeID
WHERE
  (inserted.PassRangeFirst >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeFirst <= tblPassRange.PassRangeLast)
  OR
  (inserted.PassRangeLast >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeLast <= tblPassRange.PassRangeLast)


I'm guessing the problem is with the OR short circuiting your logic, you probably want a few more brackets in there.

Try this:


SELECT COUNT(*)
FROM
inserted
INNER JOIN
tblPassRange
ON inserted.TicketTypeID = tblPassRange.TicketTypeID
WHERE
  ((inserted.PassRangeFirst >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeFirst <= tblPassRange.PassRangeLast))
  OR
  ((inserted.PassRangeLast >= tblPassRange.PassRangeFirst) AND (inserted.PassRangeLast <= tblPassRange.PassRangeLast))
0
 

Author Comment

by:raswa
ID: 17136722
Figured it out....
That'll teach me to read the help files.

The problem is that the inserted table and the real table both contain the inserted values.

The way around it is to have an identity field, and then add the following to the if statements...
inserted.PassRangeID <> tblPassRange.PassRangeID.

It seem's to work now...
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17304987
Post a comment in the community support to close this question as you figured out the answer.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17348707
Closed, 125 points refunded.
ee ai construct
Community Support Moderator
replacement part #xm34
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 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

23 Experts available now in Live!

Get 1:1 Help Now