Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

trigger fails when updating >1 row

Posted on 2012-03-12
4
Medium Priority
?
290 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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview

783 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