Solved

trigger fails when updating >1 row

Posted on 2012-03-12
4
278 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
Comment Utility
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 500 total points
Comment Utility
Do you have any triggers on Appeals table or other triggers on Parcel table?
0
 
LVL 25

Expert Comment

by:jogos
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now