SQL存储过程,变量为null

时间:2014-01-08 20:36:14

标签: mysql sql

这是我的存储过程:

CREATE PROCEDURE `insert_exercise` (nazev VARCHAR(45), URL VARCHAR(255), obtiznost INT, partie INT)

BEGIN
    DECLARE video_id INT;
    DECLARE cvik_id INT;

    INSERT INTO `odkaz_video` (URL) VALUES (@URL);
    SELECT id INTO video_id FROM `odkaz_video` WHERE URL = @URL;
    INSERT INTO `cvik` (nazev, odkaz_video, obtiznost_id) VALUES (@nazev, video_id, @obtiznost);
    SELECT id INTO cvik_id FROM `cvik` WHERE nazev = @nazev;
    INSERT INTO `cvik_partie` (partie_id, cvik_id) VALUES (@partie, cvik_id);
END

当我调用此SP时,我收到错误1048,列'URL'不能为空。为什么即使我输入了所有参数,这个变量(@URL)为空?

感谢您的回复

2 个答案:

答案 0 :(得分:1)

使用过程参数中给出的名称:

CREATE PROCEDURE insert_exercise nazev VARCHAR(45),网址 VARCHAR(255), obtiznost INT, partie INT)

它们没有前缀 @

查看本教程:MySQL stored procedure parameters

答案 1 :(得分:0)

@URL为NULL,因为您从未设置其值。输入参数名为URL,而不是@URL

此外,我建议您使用p_作为参数名称的前缀,并使用v_作为本地变量名称的前缀,以区别于列名称。

例如:

CREATE PROCEDURE `insert_exercise` (p_nazev VARCHAR(45), p_URL VARCHAR(255), p_obtiznost INT, p_partie INT)

BEGIN
    DECLARE v_video_id INT;
    DECLARE v_cvik_id INT;

    INSERT INTO `odkaz_video` (URL) VALUES (p_URL);
    SELECT id INTO v_video_id FROM `odkaz_video` WHERE URL = p_URL;
    INSERT INTO `cvik` (nazev, odkaz_video, obtiznost_id) VALUES (p_nazev, v_video_id, p_obtiznost);
    SELECT id INTO v_cvik_id FROM `cvik` WHERE nazev = p_nazev;
    INSERT INTO `cvik_partie` (partie_id, cvik_id) VALUES (p_partie, v_cvik_id);
END