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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:03:55+00:00 2026-05-21T10:03:55+00:00

I need to have a function that increments the certain ID in a table

  • 0

I need to have a function that increments the certain ID in a table (like auto_increment)

I have smth like this

DELIMITER $$

DROP FUNCTION IF EXISTS `GetNextID`$$

CREATE FUNCTION `GetNextID`(tblName TEXT, increment INT) 
RETURNS INT 
DETERMINISTIC
    BEGIN
        DECLARE NextID INT;
        SELECT MAX(concat(tblName, 'ID')) + increment INTO NextID FROM concat('table_', tblName);
        ## SELECT MAX(articleID) + increment INTO NextID FROM table_article;
        RETURN NextID;
    END$$

DELIMITER ;

INSERT INTO `table_article` ( articleID, articleAlias ) VALUES ( GetNextID('article', 5), 'TEST' );

So i pass two variables: tblName (without table_ prefix), and the increment number. The commented line – SELECT query inside the function itself – works well, but i want to dynamically pass table name to the function and so get data from a certain col of certain table. What am I doing wrong?

The error is:

#1064 - 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 '('table_', tblName);
RETURN NextID;
END' at line 6 

if i simply try to select max value in such a way

SELECT MAX(articleID) + increment INTO NextID FROM tblName;

The error reports that tblName does not exist. How can i tell MySql that this is actually a var passed to the function, not an exact table name? If it is possible.

  • 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-21T10:03:56+00:00Added an answer on May 21, 2026 at 10:03 am

    you need something like

    prepare stmp from concat('SELECT MAX(ID) + ', increment, ' INTO NextID FROM table_', tblName); 
    execute stmp; 
    deallocate prepare stmp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in delphi7 i have a function that i need to return a array as
I have two classes that each need an instance of each other to function.
i need help with disk_total_space function.. i have this on my code <?php $sql=select
$('input[type=checkbox]').unbind().click(function(e){ $(this).attr('checked', true) return false; }); I NEED to return false because I have
I need to have a routine/function/stored proc/whatever to archive particular accounts from our 'active'
I have a function where I need to do something to a string. I
I have a connection string being passed to a function, and I need to
I have need to select a number of 'master' rows from a table, also
I have a function that calls itself to increment and decrement a stack. I
We have need for a rating system in a project we are working on,

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.