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

  • SEARCH
  • Home
  • 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 8875821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:04:45+00:00 2026-06-14T19:04:45+00:00

The stored procedure is goin to take in a firstName, lastName, currentPostalCode and newPostalCode

  • 0

The stored procedure is goin to take in a firstName, lastName, currentPostalCode and newPostalCode as arguements. I want to update the postalCode column only if there is one person with the given firstName, lastName and currentPostalCode.

How is this done? Do you use sub queries or the MySQL IF() or something else?

Thanks.

  • 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-14T19:04:47+00:00Added an answer on June 14, 2026 at 7:04 pm

    You don’t need any fancy conditional logic to perform this action, just a regular UPDATE statement with a WHERE clause will do it. If no rows is matched by the WHERE clause, no update takes place.

    /* Update the records with matching name & postcode */
    UPDATE  yourtable
      SET postalCode = 'newPostalCode'
    WHERE
      /* If all 3 conditions don't match, nothing gets updated */
      firstName = 'firstName'
      AND lastName = 'lastName'
      AND postalCode = 'oldPostalCode'
    

    As a stored procedure:

    CREATE PROCEDURE updatePostalCode (
      IN in_firstName VARCHAR(64),
      IN in_lastName VARCHAR(64),
      IN in_oldPostalCode VARCHAR(16),
      IN in_newPostalCode VARCHAR(16)
    )
    BEGIN
      UPDATE yourtable 
      SET
        postalCode = in_newPostalCode 
      WHERE 
        lastName = in_lastName 
        AND firstName = in_firstName 
        AND postalCode = in_oldPostalCode;
    END
    

    Addendum: If you really only want to do this if there is exactly one match…

    To update only in the event of exactly one matching row (not 2 or more), you can still use a plain update statement, with an additional subquery in the WHERE clause

    UPDATE  yourtable
      SET postalCode = 'newPostalCode'
    WHERE
      /* If all 3 conditions don't match, nothing gets updated */
      firstName = 'firstName'
      AND lastName = 'lastName'
      AND postalCode = 'oldPostalCode'
      /* Result of count subquery = 1 */
      AND (
        SELECT COUNT(*) AS matched 
        FROM yourtable 
        WHERE firstName = 'firstName' AND lastName = 'lastName' AND postalCode = 'oldPostalCode'
      ) = 1
    

    Addendum 2 To exit with error if there is not exactly one match:

    CREATE PROCEDURE updatePostalCode (
      IN in_firstName VARCHAR(64),
      IN in_lastName VARCHAR(64),
      IN in_oldPostalCode VARCHAR(16),
      IN in_newPostalCode VARCHAR(16)
    )
    BEGIN
      DECLARE num_matched INT;
      SET num_matched = (SELECT COUNT(*) FROM yourtable WHERE firstName = 'firstName' AND lastName = 'lastName' AND postalCode = 'oldPostalCode');
    
      IF (num_matched <> 1) THEN 
         /* Trigger an error and exit */
         LEAVE updatePostalCode
      END IF
    
      /* Perform the UPDATE statement */
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an insert stored procedure likes this insert into Profile_Master(FirstName,LastName,Dob,Gender,MobileNo,Country,State,EmailId,Password) values (@FirstName,@LastName,@Dob,@Gender,@MobileNo,@Country,@State,@EmailId,@Password) set
This stored procedure doesn't update the column FailedLoginAttempts if the conditions are true. Can
This stored procedure is supposed to : Take the userid and Date as input
I have a stored procedure which update a table based on such calculation and
The stored procedure builds without any problem. The purpose of this is to take
In this stored procedure I want to get list of all the tables of
There is a stored procedure that we're trying to increase the performance of... at
A stored procedure returns data about a user record including a nullable datetime column
My stored procedure looks like: WITH MYCTE(....) AS ( ... ) UPDATE ... (using
My stored procedure returns this 1 skl meter 50 I want my c# program

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.