Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

trigger fails when updating >1 row

Posted on 2012-03-12
4
Medium Priority
?
288 Views
Last Modified: 2012-03-13
Why would this trigger succeed if there's only one row to be updated, but it fails if there's >1 row to be updated?

IF EXISTS (select null
      from   Appeals a INNER JOIN
         INSERTED i ON a.ParcelNo = i.ParcelNo
                AND a.TaxYear = i.TaxYear
                AND a.LValue = i.CertValue
                AND a.LAssmt = i.CertAssmt)
UPDATE Appeals SET Ruling = 'AFFIRMED'
FROM   Appeals as a INNER JOIN
             INSERTED AS i on a.ParcelNo = i.ParcelNo
            AND a.TaxYear = i.TaxYear
                                   AND a.LValue = i.CertValue
                                   AND a.LAssmt = i.CertAssmt

The error message I get is:
Subquery returned more than one value. This is not permitted when the subquery follows =, !=, etc. or when the subquery is used as an expression.

Sorry this is all text, but I can't figure out how to post code and text separately in the new EE. :(
0
Comment
Question by:GordonPrince
[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
  • 2
4 Comments
 
LVL 4

Author Comment

by:GordonPrince
ID: 37712193
This is pretty similar, and a bit more economical. It seems to have the same behavior. I don't understand why this one fails either.

TRIGGER AFTER UPDATE OF Parcel...
-------------------------------------
UPDATE Appeals SET Ruling = 'AFFIRMED'
FROM   Appeals as a INNER JOIN
         INSERTED AS i on a.ParcelNo = i.ParcelNo
                               AND a.TaxYear = i.TaxYear                                                       AND a.LValue = i.CertValue
                   AND a.LAssmt = i.CertAssmt
WHERE Ruling IS NULL
   OR Ruling <> 'AFFIRMED'
0
 
LVL 13

Accepted Solution

by:
dwkor earned 2000 total points
ID: 37712282
Do you have any triggers on Appeals table or other triggers on Parcel table?
0
 
LVL 25

Expert Comment

by:jogos
ID: 37713369
The trigger handles all updated rows at same time. The join-condition between the 'inserted' (updated rows) and the appeals table is simply not unique.

This problem occurs only when that join returns multiple records, so for another multiple record update it could be the join returns only one row per record you want to update.

Try to fake that update-join with a select-join but now not from 'inserted' (that's obvious only available in trigger) but from real table with a
WHERE PK in ([list of PK's of updated records])

Other possibility To debug create a table that can contain the result of the join and replace the updat in your trigger with to insert the records to that table and do your update again, then analyse why the join returns to much.
 


PS to make a EE code-bloc click on 'code' in the menu just on top of the textbox you are tiyping in.
0
 
LVL 4

Author Comment

by:GordonPrince
ID: 37714745
The error was being caused by another trigger on the Appeals table. From my application it was just showing me the error. When I ran update statements from SQL Management Console I could see the name of the trigger that was causing the trouble. Good thinking. Thanks.

PS: I found out there's an issue with EE and IE9, for which EE has posted a fix.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 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