Solved

trigger problem using inserted table on insert or update

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

691 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