Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:35:58+00:00 2026-06-10T02:35:58+00:00

I need to update a record IFF there is only one record matching my

  • 0

I need to update a record IFF there is only one record matching my search criteria. Here’s what I have, but it’s crude:

DECLARE @TestCount INT;

SELECT @TestCount = COUNT(*)
FROM TestRecords tr
WHERE
    tr.UnitSerial = @UnitSerial
      AND
    tr.PassFailStatus = 1;

IF (@TestCount = 1)
UPDATE
    TestRecords
SET
    Invalid = 1
WHERE
    TestRecordID = 
           (SELECT TestRecordID
            FROM TestRecords tr
            WHERE
            tr.UnitSerial = @UnitSerial
              AND
            tr.PassFailStatus = 1);

Of course this is example code – there are more restrictions and tables joins, etc in the SELECT statement, and it’s all wrapped by a transaction, but this is the gist of the stored proc logic.

I’m thinking there has to be a better way but I don’t know what that is. Any suggestions?

Thanks, Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:35:59+00:00Added an answer on June 10, 2026 at 2:35 am

    You could do it in one query as:

    with toupdate as (
         select tr.*,
                count(*) over () as cnt
         from TestRecords tr
         where tr.UnitSerial = @UnitSerial AND tr.PassFailStatus = 1
        )
    update toupdate
        set Invalid = 1
        where cnt = 1
    

    This assumes you are using SQL 2005 or greater.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need this query - update last but one record. UPDATE changes SET checked=''
I need to update a record in the datastore, but instead of updated record
I need to update the primary key for a record but it's also the
I need help extending a functional update query that performs calculations on one record
I have an access database that I need to update only if my information
I need to update the primary ID value of a record in table_1 and
I have a table in mnesia and I need to update individual fields in
I need to update a record in a database with the following fields [ID]
I need some SQL to update a record in a database if it exists
Is there a way to update more than one Database having same schema using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.