Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

sql - How can I send a table as a parameter to stored procedure in MySQL?

I have a stored procedure in MySQL like:

DELIMITER $$
CREATE PROCEDURE addIns(IN insCode VARCHAR(21), IN insName VARCHAR(21), IN stat BIT(1))
BEGIN
    INSERT INTO institution(institution.institution_code, institution.institution_name, institution.is_active) 
    VALUES (insCode, insName, stat);
END$$
DELIMITER ;

But I want like this:

DELIMITER $$
CREATE PROCEDURE addIns(IN _institution)
BEGIN
    INSERT INTO institution(institution.institution_code, institution.institution_name, institution.is_active)
    VALUES (_institution.institution_code, _institution.institution_name, _institution.is_active);
END$$
DELIMITER ;

Because I want to use this stored procedure in Java Spring App. How can I provide it?

My instutuion table is:

enter image description here

question from:https://stackoverflow.com/questions/65541366/how-can-i-send-a-table-as-a-parameter-to-stored-procedure-in-mysql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
CREATE PROCEDURE addIns(IN _institution)
BEGIN
    @sql := CONCAT( 'INSERT INTO institution (institution_code, institution_name, is_active) SELECT institution_code, institution_name, is_active FROM ' , _institution);
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DROP PREPARE stmt;
END

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...