我的第一个存储过程中的错误 - 帮助!

时间:2011-04-19 19:34:30

标签: mysql stored-procedures

这是我的第一个存储过程。我觉得很难调试它。花一点时间来帮助我

create procedure myworld.perform_target_proc(
  IN inp_usr_id integer,
  IN inp_tgt_src_id integer,
  IN inp_tgt_src_type varchar(30),
  IN inp_tgt_usr_id integer,
  IN tgt_usr_msg text,
  out tgt_res varchar(30)
)
BEGIN
  declare target_count integer
  select count(target_id) from target where usr_id=inp_usr_id and tgt_src_id=inp_tgt_src_id and tgt_src_type=inp_tgt_src_type
and tgt_usr_id=inp_tgt_usr_id into target_count
  if target_count=0 then
    begin
    insert into target(usr_id, tgt_src_id, tgt_src_type, tgt_usr_id, tgt_usr_msg) values
    (inp_usr_id, inp_tgt_src_id, inp_tgt_src_type, inp_tgt_usr_id, inp_tgt_usr_msg)
    set tgt_res = 'new target created'
    end
  else
    set tgt_res = 'target already exist'
  end if
END |

2 个答案:

答案 0 :(得分:2)

看起来你错过了一些分号。

create procedure myworld.perform_target_proc(
  IN inp_usr_id integer,
  IN inp_tgt_src_id integer,
  IN inp_tgt_src_type varchar(30),
  IN inp_tgt_usr_id integer,
  IN tgt_usr_msg text,
  out tgt_res varchar(30)
)
BEGIN
  declare target_count integer;
  select count(target_id) from target where usr_id=inp_usr_id and tgt_src_id=inp_tgt_src_id and tgt_src_type=inp_tgt_src_type
and tgt_usr_id=inp_tgt_usr_id into target_count;
  if target_count=0 then
    insert into target(usr_id, tgt_src_id, tgt_src_type, tgt_usr_id, tgt_usr_msg) values
    (inp_usr_id, inp_tgt_src_id, inp_tgt_src_type, inp_tgt_usr_id, inp_tgt_usr_msg)
    set tgt_res = 'new target created';
  else
    set tgt_res = 'target already exist';
  end if;
END |

答案 1 :(得分:0)

我是一个SQL Server人,而不是MySQL人,但在SQL Server中,这甚至不会编译:变量需要@前缀一件事。我想你想要

set @target_count = SELECT count(target_id)....

或更好地完全避免使用局部变量。