Problems with MySQL stored procedure

I am trying to compile the following (see below) MySQL stored procedure and I keep getting Error Code 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 ” at line 3 0.000 sec”

The purpose of the procedure is to cycle through menu_input table to gather parameters for building insert queries to populate data_ouput table.

The error line seems broken as it locates the error at the beginning of the script whereas that same beginning which is related to variables declaration compiles by himself. Also, if I’m not wrong, the error suggests using server syntax; I tried reviewing each statement of my code, comparing it to official syntax but found no deviation (also due to my limited experience).

The script follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE PROCEDURE SodAnalysis()
BEGIN
DECLARE strFieldName1 VARCHAR(100);
DECLARE strFieldName2 VARCHAR(100);
DECLARE strSQL VARCHAR(100);
DECLARE rischio VARCHAR(100); -- Assuming these parameters need to be declared
DECLARE categoria VARCHAR(100); -- and assigned values before executing @strSQL
-- Cursor to iterate through menu_input records
DECLARE menu_cursor CURSOR FOR
SELECT
menu1,
menu2,
rischio,
categoria
FROM menu_input;
OPEN menu_cursor;
FETCH NEXT FROM menu_cursor INTO strFieldName1, strFieldName2, rischio, categoria; -- Adjusted to fetch @rischio and @categoria as well
WHILE @@FETCH_STATUS = 0 DO
-- Construct the SQL query with parameters A and B
SET strSQL = CONCAT('
INSERT INTO data_output
SELECT
c.*,
d.funzione AS funzione2,
d.processo AS processo2
FROM (
SELECT
a.*,
b.funzione AS funzione1,
b.processo AS processo1
FROM (
SELECT
t1.id AS id1,
t2.id AS id2,
t1.utente_cod,
t1.utente_des,
t1.societa AS societa1,
t1.societa_des AS societa_des1,
t2.societa AS societa2,
t2.societa_des AS societa_des2,
t1.ruolo_cod AS ruolo_cod1,
t1.ruolo_des AS ruolo_des1,
t2.ruolo_cod AS ruolo_cod2,
t2.ruolo_des AS ruolo_des2,
t1.modulo AS modulo1,
t2.modulo AS modulo2,
t1.modulo_des AS modulo_des1,
t2.modulo_des AS modulo_des2,
t1.menu_funz AS menu_funz1,
t2.menu_funz AS menu_funz2,
t1.progr_cod AS progr_cod1,
t2.progr_cod AS progr_cod2,
@rischio AS rischio,
@categoria AS categoria
FROM
data_input t1
INNER JOIN
data_input t2
ON
t1.utente_cod = t2.utente_cod
WHERE
t1.utente_cod IN (
SELECT utente_cod
FROM data_input
WHERE menu_funz = @strFieldName1
)
AND t1.utente_cod IN (
SELECT utente_cod
FROM data_input
WHERE menu_funz = @strFieldName2
)
AND t1.menu_funz = @strFieldName1
AND t2.menu_funz = @strFieldName2
) a
LEFT JOIN
transcodifica_input b
ON
a.menu_funz1 = b.MENU
) c
LEFT JOIN
transcodifica_input d
ON
c.menu_funz2 = d.MENU;
');
-- Execute the constructed SQL query
PREPARE stmt from StrSQL;
SET @rischio = rischio;
SET @categoria = categoria;
SET @strFieldName1 = strFieldName1;
SET @strFieldName2 = strFieldName2;
EXECUTE stmt USING @rischio, @categoria, @strFieldName1, @strFieldName2;
deallocate prepare stmt;
-- Move to the next record
FETCH NEXT FROM menu_cursor INTO strFieldName1, strFieldName2, rischio, categoria; -- Adjusted to fetch @rischio and @categoria as well
END WHILE;
-- Close and deallocate the cursor
CLOSE menu_cursor;
DEALLOCATE menu_cursor;
END;
GO
</code>
<code>CREATE PROCEDURE SodAnalysis() BEGIN DECLARE strFieldName1 VARCHAR(100); DECLARE strFieldName2 VARCHAR(100); DECLARE strSQL VARCHAR(100); DECLARE rischio VARCHAR(100); -- Assuming these parameters need to be declared DECLARE categoria VARCHAR(100); -- and assigned values before executing @strSQL -- Cursor to iterate through menu_input records DECLARE menu_cursor CURSOR FOR SELECT menu1, menu2, rischio, categoria FROM menu_input; OPEN menu_cursor; FETCH NEXT FROM menu_cursor INTO strFieldName1, strFieldName2, rischio, categoria; -- Adjusted to fetch @rischio and @categoria as well WHILE @@FETCH_STATUS = 0 DO -- Construct the SQL query with parameters A and B SET strSQL = CONCAT(' INSERT INTO data_output SELECT c.*, d.funzione AS funzione2, d.processo AS processo2 FROM ( SELECT a.*, b.funzione AS funzione1, b.processo AS processo1 FROM ( SELECT t1.id AS id1, t2.id AS id2, t1.utente_cod, t1.utente_des, t1.societa AS societa1, t1.societa_des AS societa_des1, t2.societa AS societa2, t2.societa_des AS societa_des2, t1.ruolo_cod AS ruolo_cod1, t1.ruolo_des AS ruolo_des1, t2.ruolo_cod AS ruolo_cod2, t2.ruolo_des AS ruolo_des2, t1.modulo AS modulo1, t2.modulo AS modulo2, t1.modulo_des AS modulo_des1, t2.modulo_des AS modulo_des2, t1.menu_funz AS menu_funz1, t2.menu_funz AS menu_funz2, t1.progr_cod AS progr_cod1, t2.progr_cod AS progr_cod2, @rischio AS rischio, @categoria AS categoria FROM data_input t1 INNER JOIN data_input t2 ON t1.utente_cod = t2.utente_cod WHERE t1.utente_cod IN ( SELECT utente_cod FROM data_input WHERE menu_funz = @strFieldName1 ) AND t1.utente_cod IN ( SELECT utente_cod FROM data_input WHERE menu_funz = @strFieldName2 ) AND t1.menu_funz = @strFieldName1 AND t2.menu_funz = @strFieldName2 ) a LEFT JOIN transcodifica_input b ON a.menu_funz1 = b.MENU ) c LEFT JOIN transcodifica_input d ON c.menu_funz2 = d.MENU; '); -- Execute the constructed SQL query PREPARE stmt from StrSQL; SET @rischio = rischio; SET @categoria = categoria; SET @strFieldName1 = strFieldName1; SET @strFieldName2 = strFieldName2; EXECUTE stmt USING @rischio, @categoria, @strFieldName1, @strFieldName2; deallocate prepare stmt; -- Move to the next record FETCH NEXT FROM menu_cursor INTO strFieldName1, strFieldName2, rischio, categoria; -- Adjusted to fetch @rischio and @categoria as well END WHILE; -- Close and deallocate the cursor CLOSE menu_cursor; DEALLOCATE menu_cursor; END; GO </code>
CREATE PROCEDURE SodAnalysis()
BEGIN
    DECLARE strFieldName1 VARCHAR(100);
    DECLARE strFieldName2 VARCHAR(100);
    DECLARE strSQL VARCHAR(100);
    DECLARE rischio VARCHAR(100); -- Assuming these parameters need to be declared
    DECLARE categoria VARCHAR(100); -- and assigned values before executing @strSQL

    -- Cursor to iterate through menu_input records
    DECLARE menu_cursor CURSOR FOR
    SELECT 
        menu1, 
        menu2, 
        rischio, 
        categoria
    FROM menu_input;

    OPEN menu_cursor;
    FETCH NEXT FROM menu_cursor INTO strFieldName1, strFieldName2, rischio, categoria; -- Adjusted to fetch @rischio and @categoria as well

    WHILE @@FETCH_STATUS = 0 DO
        -- Construct the SQL query with parameters A and B
        SET strSQL = CONCAT('
            INSERT INTO data_output 
            SELECT 
                c.*, 
                d.funzione AS funzione2, 
                d.processo AS processo2 
            FROM (
                SELECT 
                    a.*, 
                    b.funzione AS funzione1, 
                    b.processo AS processo1 
                FROM (
                    SELECT 
                        t1.id AS id1, 
                        t2.id AS id2, 
                        t1.utente_cod, 
                        t1.utente_des, 
                        t1.societa AS societa1, 
                        t1.societa_des AS societa_des1, 
                        t2.societa AS societa2, 
                        t2.societa_des AS societa_des2, 
                        t1.ruolo_cod AS ruolo_cod1, 
                        t1.ruolo_des AS ruolo_des1, 
                        t2.ruolo_cod AS ruolo_cod2, 
                        t2.ruolo_des AS ruolo_des2, 
                        t1.modulo AS modulo1, 
                        t2.modulo AS modulo2, 
                        t1.modulo_des AS modulo_des1, 
                        t2.modulo_des AS modulo_des2, 
                        t1.menu_funz AS menu_funz1, 
                        t2.menu_funz AS menu_funz2, 
                        t1.progr_cod AS progr_cod1, 
                        t2.progr_cod AS progr_cod2, 
                        @rischio AS rischio, 
                        @categoria AS categoria 
                    FROM 
                        data_input t1
                    INNER JOIN 
                        data_input t2 
                    ON 
                        t1.utente_cod = t2.utente_cod 
                    WHERE 
                        t1.utente_cod IN (
                            SELECT utente_cod 
                            FROM data_input 
                            WHERE menu_funz = @strFieldName1
                        ) 
                        AND t1.utente_cod IN (
                            SELECT utente_cod 
                            FROM data_input 
                            WHERE menu_funz = @strFieldName2
                        ) 
                        AND t1.menu_funz = @strFieldName1 
                        AND t2.menu_funz = @strFieldName2
                ) a 
                LEFT JOIN 
                    transcodifica_input b 
                ON 
                    a.menu_funz1 = b.MENU
            ) c 
            LEFT JOIN 
                transcodifica_input d 
            ON 
                c.menu_funz2 = d.MENU;
        ');

        -- Execute the constructed SQL query
        PREPARE stmt from StrSQL;
        SET @rischio = rischio;
        SET @categoria = categoria;
        SET @strFieldName1 = strFieldName1;
        SET @strFieldName2 = strFieldName2;
        EXECUTE stmt USING @rischio, @categoria, @strFieldName1, @strFieldName2;
        deallocate prepare stmt;

        -- Move to the next record
        FETCH NEXT FROM menu_cursor INTO strFieldName1, strFieldName2, rischio, categoria; -- Adjusted to fetch @rischio and @categoria as well
    END WHILE;

    -- Close and deallocate the cursor
    CLOSE menu_cursor;
    DEALLOCATE menu_cursor;
END;
GO

I wish you the best hoping anyone could give a hand, Best Regards, David

I tried reviewing each statement of my code, comparing it to official syntax but found no deviation (also due to my limited experience)

New contributor

user25436251 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật