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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:05:34+00:00 2026-05-26T01:05:34+00:00

Am using MySQL 5 on OS X – Snow Leopard… Have working code in

  • 0

Am using MySQL 5 on OS X – Snow Leopard…

Have working code in place which obtains the highest sequence number ID from a sequence table and then increments and assigns it to its corresponding table:

The original code’s purpose is to dynamically increments a specific table’s last sequence id and set its corresponding table’s id to that new value.

Notes:


1. Original Code Snippet (which is working):

Get last sequence number

replace into my_sequence_id_s set id = 
(select max(CONVERT(sequence_id, signed)) from my_table_t);

Increments the number

insert into my_sequence_id_s set id = null;

Saves the number as a variable

set @dynamicId = last_insert_id();

Print

select @dynamicId;

2. Refactoring:

DROP PROCEDURE IF EXISTS generate_dynamic_id#

CREATE PROCEDURE generate_dynamic_id

(IN _sequence_table varchar(40),
 IN _actual_table varchar(40), 
 IN _id_field VARCHAR(40), 
 OUT dynamic_id varchar(40))

 BEGIN
  -- Get Last Sequence Number
     set @getLastSequenceNumberSQL = 
     concat('REPLACE INTO ', _sequence_table, 'SET ID = 
     (select max(CONVERT(',_id_field,', signed)) 
      from ', _actual_table, ');');
      prepare lastRecordStmt from @getLastSequenceNumberSQL;
      execute lastRecordStmt;
      deallocate prepare lastRecordStmt;

  -- Increments the number.
      set @createNewSequenceNumberSQL = 
      concat('insert into ', _sequence_table ,' set id = null;');
      prepare newSequenceNumberStmt from @createNewSequenceNumberSQL;
      execute newSequenceNumberStmt;
      deallocate prepare newSequenceNumberStmt;

   -- Set the number as a dynamic variable.
      set @dynamic_id = last_insert_id();
END;
#

3. Here’s the calling function (which fails):

— Get dynamically incremented id

call generate_dynamic_id(
   'my_sequence_id_s', 'my_table_t', 'table_id', @dynamicId);

Error:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 

You have an error in your SQL syntax; 

check the manual that corresponds to your MySQL server version

for the right syntax to use near

'ID = (select max(CONVERT(id_field, signed)) from my_table_t)' at line 1

For some odd reason, dynamic function calls are not allowed in Stored Functions or Triggers, so that’s why a Stored Procedure was used.

As you can see, I am setting up varchars at the parameters and then trying to concatenate them as strings and run them inside prepared statements.

Any help would be greatly appreciated…

  • 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-05-26T01:05:35+00:00Added an answer on May 26, 2026 at 1:05 am

    concat(‘REPLACE INTO ‘, _sequence_table, ‘SET ID =

    where space between _sequence_table and SET ID?

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

Sidebar

Related Questions

Note: Using MySQL 4.0, which means no subqueries (at present). I have 2 tables:
I am using MySQL and PHP for a project I am working. I have
I am using mysql which i have a stored procedure which has an input
I am using MySQl , I have a table named cars which is in
(Using MySQL and PHP) I have a search form that will allow my users
Using MySQL, I have three tables: projects : ID name 1 birthday party 2
Using mysql, c#.net, Java Script I have the web application, i want to create
Using MySql 5, I have a task where I need to update one table
using mysql and php, I have two tables, I'm french so the table names
Using MySQL Administrator GUI tool I have exported some data tables retrieved from an

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.