Solved

trigger problem using inserted table on insert or update

Posted on 2006-07-19
5
586 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

937 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

4 Experts available now in Live!

Get 1:1 Help Now