Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [MariaDB] Settare AUTO_INCREMENT da variabile

    ciao!

    avrei la necessità di settare il valore iniziale di AUTO_INCREMENT di una tabella in maniera dinamica.
    ho provato a creare questa stored procedure:
    codice:
    DELIMITER //
    DROP PROCEDURE IF EXISTS set_articoli_ecomm_auto_incr //
    CREATE PROCEDURE set_articoli_ecomm_auto_incr()
        NO SQL
    BEGIN
        DECLARE ai INT DEFAULT 0;
    
        SET ai = (SELECT MAX(product_id) + 1 FROM oc_product);
    
        ALTER TABLE articoli_ecommerce
            AUTO_INCREMENT = ai;
    END
    ma mi da errore:
    codice:
    [42000][1064] (conn=1315390) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ai; [42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ai; END' at line 9
    come posso fare??

  2. #2
    Utente di HTML.it L'avatar di Joe Taras
    Registrato dal
    Nov 2003
    residenza
    Taranto
    Messaggi
    954
    Quote Originariamente inviata da fermat Visualizza il messaggio
    ciao!

    avrei la necessità di settare il valore iniziale di AUTO_INCREMENT di una tabella in maniera dinamica.
    ho provato a creare questa stored procedure:
    codice:
    DELIMITER //
    DROP PROCEDURE IF EXISTS set_articoli_ecomm_auto_incr //
    CREATE PROCEDURE set_articoli_ecomm_auto_incr()
        NO SQL
    BEGIN
        DECLARE ai INT DEFAULT 0;
    
        SET ai = (SELECT MAX(product_id) + 1 FROM oc_product);
    
        ALTER TABLE articoli_ecommerce
            AUTO_INCREMENT = ai;
    END
    ma mi da errore:
    codice:
    [42000][1064] (conn=1315390) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ai; [42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ai; END' at line 9
    come posso fare??
    Ciao, non puoi mettere uno statement DDL in una S.P. in quel modo, devi wrapparlo iin un prepared statement.

    Puoi trovare qui un ottimo esempio:

    https://dba.stackexchange.com/questi...ncrement-value

    Vedi la risposta con il check verde

  3. #3
    grazie per la dritta!

    così ha funzionato:
    codice:
    DELIMITER //
    DROP PROCEDURE IF EXISTS set_articoli_ecomm_auto_incr //
    CREATE PROCEDURE set_articoli_ecomm_auto_incr()
        NO SQL
    BEGIN
        DECLARE ai INT DEFAULT 0;
    
        SET ai = (SELECT MAX(product_id) + 1 FROM oc_product);
    
        SET @query = CONCAT("ALTER TABLE articoli_ecommerce AUTO_INCREMENT = ", ai, " ");
        PREPARE comando FROM @query;
        EXECUTE comando;
        DEALLOCATE PREPARE comando;
    END

  4. #4
    Utente di HTML.it L'avatar di Joe Taras
    Registrato dal
    Nov 2003
    residenza
    Taranto
    Messaggi
    954
    Quote Originariamente inviata da fermat Visualizza il messaggio
    grazie per la dritta!

    così ha funzionato:
    codice:
    DELIMITER //
    DROP PROCEDURE IF EXISTS set_articoli_ecomm_auto_incr //
    CREATE PROCEDURE set_articoli_ecomm_auto_incr()
        NO SQL
    BEGIN
        DECLARE ai INT DEFAULT 0;
    
        SET ai = (SELECT MAX(product_id) + 1 FROM oc_product);
    
        SET @query = CONCAT("ALTER TABLE articoli_ecommerce AUTO_INCREMENT = ", ai, " ");
        PREPARE comando FROM @query;
        EXECUTE comando;
        DEALLOCATE PREPARE comando;
    END
    Ottimo!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.