Solved

trigger problem using inserted table on insert or update

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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