sqlentity-framework-coremariadbsequence-sqlmariadb-10.3

Pass SELECT MAX(`Id`) FROM Table to setval()


I want to pass (SELECT MAX(Id) FROM Table to mariadb's setval() function I tried with:

SELECT setval(`MySequence`, (SELECT MAX(`Id`) FROM `Table`));

but it doesn't work, I also tried:

SET @max_value = (SELECT MAX(`Id`) FROM `Table`);
SELECT setval(`MySequence`, @max_value);

how am I supposed to do this?

EDIT I made a mistake posting the question. I was using SET on the second code and is not working

EDIT As I said on the comments I'm trying to do this just once, executing from an Entity Framework Core migration. What I ended doing is executing the SELECT MAX(Id) FROM Table and recovering that value from the migration code to interpolate it later on the $"SELECT setval('sequence', {value}, true)"


Solution

  • I found working workaround using prepared statement:

    SET @max_value = (SELECT MAX(`Id`) FROM `Table`);
    EXECUTE IMMEDIATE CONCAT('SELECT SETVAL(`MySequence`, ', @max_value, ')');