存储过程出错 - 列数与值不匹配

时间:2011-03-12 14:52:07

标签: mysql sql database

我已经创建了一个存储过程:

DELIMITER $$
DROP PROCEDURE IF EXISTS `zero`.`sp_for_insert_into_account_db`$$
CREATE PROCEDURE `zero`.`sp_for_insert_into_account_db` (usr_key char(6),usr_name varchar(15),usr_password varchar(15),OUT output_message INT)

BEGIN

DECLARE no_of_row INT;    
SELECT COUNT(*) INTO no_of_row from account_db;  
IF no_of_row < 4 THEN

    SET @s = CONCAT('insert into account_db (USR_KEY,USR_NAME,USR_PWD) VALUES (',usr_key,usr_name,usr_password,')');

    PREPARE stmt FROM @s;  
    EXECUTE stmt;    
    DEALLOCATE PREPARE stmt;    
    SET output_message=1;    
ELSE    
    SET output_message=0;       
END IF;
END$$

DELIMITER ;

我用查询

来调用它
 call sp_for_insert_into_account_db('a','b','c',@output_ message);

错误如下:

  

列数与值不匹配...

我通过了4个论点......

为什么会出现此错误?

我已经使用这种语法检查过(默认参数是IN类型)

sp_for_insert_into_account_db(IN usr_key char(6), 
                              IN usr_name varchar(15), 
                              IN usr_password varchar(15),
                              OUT output_message INT)

2 个答案:

答案 0 :(得分:1)

问题也在这里:

SET @s = CONCAT('insert into account_db (USR_KEY,USR_NAME,USR_PWD) VALUES (',usr_key,usr_name,usr_password,')');

您正在尝试插入3个值,并置连接返回1

改为使用:

SET @s = CONCAT('insert into account_db (USR_KEY,USR_NAME,USR_PWD) VALUES (\'',usr_key,'\',\'',usr_name,'\',\'',usr_password,'\')');

答案 1 :(得分:1)

当你不需要时,不完全确定你为什么使用预备语句/动态sql?请参阅以下示例,我已经为您清理了一点:

drop procedure if exists sp_for_insert_into_account_db;

delimiter #

create procedure sp_for_insert_into_account_db 
(
in p_usr_key char(6),
in p_usr_name varchar(15),
in p_usr_pwd varchar(15),
out p_output_message tinyint unsigned
)
begin

declare v_no_of_row int unsigned default 0;

set p_output_message=0;

select count(*) into v_no_of_row from account_db;

if v_no_of_row < 4  then
    insert into account_db(usr_key, usr_name, usr_pwd) values (p_usr_key, p_usr_name, p_usr_pwd);
    set p_output_message = 1;
end if;

end#

delimiter ;

call sp_for_insert_into_account_db (...);

修改

你是1970年代的COBOL程序员,你为什么要使用CAPS?