Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

trigger problem using inserted table on insert or update

Posted on 2006-07-19
5
Medium Priority
?
593 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
[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
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

597 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