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 8490323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:04:53+00:00 2026-06-10T22:04:53+00:00

Is there a way to do something like this in mysql: INSERT INTO <sometable>

  • 0

Is there a way to do something like this in mysql:

INSERT INTO <sometable> SET 
someRow = inputSomeValue, someOtherRow = inputValue2 AND
call function1()
ON DUPLICATE KEY ID = inNewID AND
call function2();

Basically, i would like to call some outer mysql stored procedure on “clean” insert, and call some other procedure if insert is obstructed by duplicate key. Is that possible in MySql?

EDIT:
Original insert IS ALREADY in stored procedure!

  • 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-10T22:04:54+00:00Added an answer on June 10, 2026 at 10:04 pm

    No. It’s not possible to call a stored procedure from an INSERT statement.

    There is a way to call a stored program from an INSERT statement, but it has to be a user defined function, for example:

    INSERT INTO sometable (col)
    SELECT IF( user_defined_function() , 'abc' , 'abc' )
    

    But that function is going to get called whether the resulting row throws a duplicate key exception or not. It’s also possible to reference a user defined function within the ON DUPLICATE KEY clause.

    INSERT INTO sometable (col1)
    SELECT IF( user_defined_function() , 'abc' , 'abc' )
    ON DUPLICATE KEY UPDATE col1 = IF( udf_duplicate() , VALUES(col1), VALUES(col1) )
    

    Note that there are issues with binary logging (row vs. statement) with those constructs, depending on what those functions do, this type of statement may not be “safe” for replication.

    So I don’t think you really want to go with this approach. If you are inserting a single row, you are probably going to be better off performing the INSERT … ON DUPLICATE KEY UPDATE … statement, and then checking the number of rows affected. An affected-rows value of 1 will tell you that a row was inserted, an affected-rows value of 2 means the row was updated, so you could conditionally call the procedure you want.

    If you always want the actions in those stored procedures called whenever any row is inserted or updated, you might consider AFTER INSERT and AFTER UPDATE triggers. (You need to test to be sure the AFTER UPDATE trigger gets fired from an ON DUPLICATE KEY UPDATE… there were issues in older versions of MySQL where an AFTER UPDATE trigger wasn’t getting called.)


    EDIT:

    The most recent update to the original question specifies this INSERT statement is performed from within a MySQL stored procedure.

    I would recommend this type of approach:

    DECLARE affected_rows INT;
    
    INSERT INTO sometable (id,foo) VALUES (123,'bar')
    ON DUPLICATE KEY UPDATE foo = VALUES(foo);
    
    SELECT ROW_COUNT() INTO affected_rows;
    
    IF affected_rows = 1 THEN
       CALL stored_procedure_after_insert(); 
    ELSE
       CALL stored_procedure_after_update(); 
    END IF;
    

    Q: So, after “update” affected rows number will be 0?

    A: If no modifications were made to any rows (by the INSERT ... ON DUPLICATE KEY UPDATE statement, the ROW_COUNT() function will return 0.

    If no rows are inserted, and a single row is updated, then the ROW_COUNT() function will return a 2.

    The difference between getting a 0 and an 2 is whether the row is actually modified or not. When the result of the update results in the row being identical to the current contents of the row (“no changes made”), then ROW_COUNT() returns a 0. If the update results in a change to the row, the ROW_COUNT() will be 2.

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

Sidebar

Related Questions

Is there a way to write something like this with Mojo::UserAgent ,having the possibility
Is there a way in java to do something like this: void fnc(void Reference_to_other_func());
Is there a better way to do something like this? This is fine but
Is there a way to choose dynamicaly an operator do something like this: var
Is there any way to crate nested union types in F#? Something like this
Is there a way to obtain something like a dictionary of all key-value pairs
JSFiddle Is there any way to make something like the X on that link
is there a way in Excel to have a formula that does something like
Is there a way to fill a Delphi array using something like array =
Is there a way to manipulate the mouse position without using System.Windows.Forms.Cursor? Something like

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.