一个MYSQL存储过程中出现的问题

woshigefantong 2007-11-09 05:00:47
我是个菜鸟,接触数据库时间不长,现在在写一个MYSQL的存储过程,遇到两个其实很小的问题,可是我在网上到处都查不到相关的信息,还请各位大虾指点一下
1.select ID from test1 where ID=ID,在存储过程中我需要把查到的哪个ID存到一个变量A中,该怎么才能实现呢?
2.在存储过程中需要象外报一些错误信息,如何在存储过程中象外报错呢?
下面是一个很弱智的存储过程,可是里面的一几个问题我死活都想不明白
create procedure test()
begin
declare charge int;
select charge =ID from test1 where ID=1; //charge =ID赋值失败,该怎么才能把ID赋值给查charge呢?
if(charge=1) then
向外报出查charge取值错误,这个我不知道该怎么实现
end;

刚学没多久,问的问题可能真的很傻,还请各位不吝指导。谢谢了
...全文
62 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzk19831231 2007-11-13
  • 打赏
  • 举报
回复
create procedure test()
begin
declare charge int;
select ID into charge from test1 where ID=1;
if charge = 1 then
select '报错信息 ';
end if;

呵呵,修正一下!
lzk19831231 2007-11-13
  • 打赏
  • 举报
回复
create procedure test()
begin
declare charge int;
select ID into charge from test1 where ID=1;
if(charge=1) then
select '报错信息';
end;
richardclydeman 2007-11-12
  • 打赏
  • 举报
回复
也可以是
SELECT `charge` INTO `ID` FROM `tbl_name` WHERE conditions;

:)
yxh007 2007-11-12
  • 打赏
  • 举报
回复
LS的也可以变为

set charge=(select id from test1 where id=1);
懒得去死 2007-11-09
  • 打赏
  • 举报
回复

delimiter ||
create procedure test()
begin
declare charge int;
select charge:=ID from test1 where ID=1; 或者 select ID from test1 where ID=1 into charge;
if charge = 1 then
select 'Error';
end if;
end||
delimiter ;

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧