Salve,
sono neofita in T-SQL.
Mi sapreste dire cosa indica il simbolo @ anteposto alle variabili?
E' indispensabile?
Grazie
Salve,
sono neofita in T-SQL.
Mi sapreste dire cosa indica il simbolo @ anteposto alle variabili?
E' indispensabile?
Grazie
indica appunto che è una variabile(e non una colonna, una tabella, un'istruzione, ecc ecc)
Grazie per la risposta.
Il problema è che quando le utilizzo sql mi da errore di sintassi, quando invece le ometto funziona tutto (almeno apparentemente) correttamente. Come si spiega?
Posto direttamente il codice (che funziona):
Questo è l'errore che ricevo se aggiungo le @:codice:CREATE PROCEDURE packinglist(IN tmp_macchina Nvarchar(20)) BEGIN DECLARE myId Int; DECLARE macchina NVarchar(20); DECLARE accessorio NVarchar(20); DECLARE nome_accessorio NVarchar(100); DECLARE no_more_rows INT DEFAULT 0; DECLARE components_cursor CURSOR FOR SELECT macchine.codice as id, regole.macchina as macchina, regole.accessorio as accessorio, macchine.modello as nome_accessorio FROM regole, macchine WHERE regole.accessorio = macchine.codice AND regole.macchina = tmp_macchina; SET max_sp_recursion_depth=255; OPEN components_cursor; read_loop: LOOP FETCH NEXT FROM components_cursor INTO myId, macchina, accessorio, nome_accessorio; IF no_more_rows THEN CLOSE components_cursor; LEAVE read_loop; END IF; BLOCK2: BEGIN DECLARE mySx INT; DECLARE no_more_rows_update INT DEFAULT 0; DECLARE components_cursor_update CURSOR FOR SET max_sp_recursion_depth=255; OPEN components_cursor_update; FETCH NEXT FROM components_cursor_update INTO mySx; IF no_more_rows_update THEN CLOSE components_cursor_update; END IF; UPDATE ".$nome_temp_table." SET dx = dx + 2 WHERE dx > mySx AND parent = tmp_macchina; UPDATE ".$nome_temp_table." SET sx = sx + 2 WHERE sx > mySx AND parent = tmp_macchina; INSERT IGNORE INTO ".$nome_temp_table." (parent, codice_componente,nome_componente,sx,dx) VALUES (tmp_macchina, accessorio,nome_accessorio,mySx + 1, mySx + 2); CLOSE components_cursor_update; END BLOCK2; call packinglist(accessorio); END LOOP read_loop; END;;;
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 '@myId int;
Inoltre mi da anche errore di sintassi quando cerco ad esempio di dichiarare una variabile di tipo TABLE:
DECLARE myTable TABLE (id int);
Perchè ha questo comportamento?
Ultima modifica di lorenz_italy; 10-11-2015 a 19:35
Ok tutto chiaro.
Resta a me sconosciuto il perchè la seguente espressione mi dia errore di sintassi:
DECLARE tmp TABLE (id int primary key);
ho provato anche
DECLARE TABLE tmp (id int primary key);
CREATE TEMPORARY TABLE tmp (id int primary key);
Le ho provate tutte ma continua a darmi errore di sintassi:
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 'DECLARE components_cursor_update CURSOR FOR
SELECT z_temp_packlist_info_' at line 40
delimiter;;
drop procedure if exists packinglist;;
CREATE PROCEDURE packinglist(IN tmp_macchina Nvarchar(20))
BEGIN
DECLARE myId int;
DECLARE macchina NVarchar(20);
DECLARE accessorio NVarchar(20);
DECLARE nome_accessorio NVarchar(100);
DECLARE no_more_rows INT DEFAULT 0;
DECLARE finito INT default 0;
DECLARE components_cursor CURSOR FOR
SELECT macchine.codice as id,
regole.macchina as macchina,
regole.accessorio as accessorio,
macchine.modello as nome_accessorio
FROM regole, macchine
WHERE regole.accessorio = macchine.codice AND regole.macchina = tmp_macchina;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET finito = 1;
SET max_sp_recursion_depth=255;
OPEN components_cursor;
FETCH NEXT FROM components_cursor INTO myId, macchina, accessorio, nome_accessorio;
ciclo: WHILE NOT finito DO
BLOCK2: BEGIN
DECLARE mySx INT;
CREATE TEMPORARY TABLE temp1 ENGINE=MEMORY as (select * from macchine);
DECLARE components_cursor_update CURSOR FOR
SELECT ".$nome_temp_table.".sx as sx FROM ".$nome_temp_table." WHERE codice_componente = tmp_macchina;
SET max_sp_recursion_depth=255;
OPEN components_cursor_update;
FETCH NEXT FROM components_cursor_update INTO mySx;
UPDATE ".$nome_temp_table." SET dx = dx + 2 WHERE dx > mySx;
UPDATE ".$nome_temp_table." SET sx = sx + 2 WHERE sx > mySx;
INSERT INTO ".$nome_temp_table." (codice_componente,nome_componente,sx,dx) VALUES (accessorio,nome_accessorio,mySx + 1, mySx + 2);
call packinglist(accessorio);
END BLOCK2;
FETCH NEXT FROM components_cursor INTO myId, macchina, accessorio, nome_accessorio;
END WHILE ciclo;
END;;
Se provo a creare la tabella dopo il declare non mi da più errore di sintassi!
Ex.
DECLARE components_cursor_update CURSOR FOR
SELECT ".$nome_temp_table.".sx as sx FROM ".$nome_temp_table." WHERE codice_componente = tmp_macchina;
CREATE TEMPORARY TABLE temp1 ENGINE=MEMORY as (select * from macchine);
A quanto ho capito tutti i declare devono andare all'inizio del begin e tutti i set devono andare dopo tutti i declare.
Ok!
Ma il problema è che io dovrei operare su una tabella che deve necessariamente essere prima settata!
Ex.
CREATE TEMPORARY TABLE temp1 ENGINE=MEMORY as (select * from macchine);
DECLARE components_cursor_update CURSOR FOR
SELECT temp1.sx as sx FROM temp1 WHERE temp1.codice_componente = tmp_macchina;
Quando io faccio declare di components_cursor_update dovrei avere già la tabella temp1 settata. Dunque se sposto la create temporary table temp1 prima del declare del cursore ottengo errore di sintassi, se invece inverto chiaramente mi dice temp1 non esiste....
Mi chiedo: non è possibile dichiarare il cursore prima e poi dopo, quando setto la temporary table, invocare cursor for select?
Sto cercando sul web ma non trovo nulla a riguardo.