• cmcfarland 51322 (10/8/2013)


    Good Morning!

    I'm trying to update a column based on a WHERE statement that uses a SELECT statement as the qualifier.

    My goal is to find duplicate records in my table, then have a value in one of the columns change so that the users can search for and view these duplicate records and decide how they'd like to deal with them.

    Here's the SELECT statement that does a nice job of finding my duplicates:

    select IDX_Invoice_Number, IDX_Vendor_Number, status from _obj_3

    group by IDX_Invoice_Number, IDX_Vendor_number, status

    having COUNT (*)>1

    Here's what I'd use to accomplish my goal, if it were possible:

    update _obj_3

    set status='7'

    where

    (

    select IDX_Invoice_Number, IDX_Vendor_Number, status from _obj_3

    group by IDX_Invoice_Number, IDX_Vendor_number, status

    having COUNT (*)>1

    )

    But, I get an error because the SELECT statement yeilds multiple returns.

    Which it will, because they're duplicates that it's finding.

    I've tried using an INNER JOIN back to itself with no luck, and just created a VIEW using my SELECT statement, hoping to be able to update the VIEW.

    No luck there, as the VIEW won't allow me to update it.

    Well, thanks taking a look, and I can't wait to see what comes back.

    Thanks!

    You were getting an error (you should always post the error message btw) because you have nothing on one side of your condition. You can't just say Where (SubQuery).

    Try this:

    update _obj_3

    set status='7'

    where IDX_Invoice_Number in

    (

    select IDX_Invoice_Number from _obj_3

    group by IDX_Invoice_Number, IDX_Vendor_number, status

    having COUNT (*)>1

    )

    You might also refer to today's article by Stefan. It deals with finding and then deleting duplicates.

    http://www.sqlservercentral.com/articles/duplicate+data/102383/[/url]

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/