MySql 简单问题 求解答..

天下如山 2014-07-03 10:37:08
sql:
1、
declare tempid int;
select tempid=id from table where tempname='需求';

2、
select @tempid:=id from table where tempname='需求';

问题:
为什么1中的变量tempid 取得是空,而用@tempid则能取到值?
...全文
150 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2014-07-03
  • 打赏
  • 举报
回复
declare tempid int; declare mycursor CURSOR for select id from table where id=tempid; set tempid=2; open mycursor; 游标应该返回id=2的数据
天下如山 2014-07-03
  • 打赏
  • 举报
回复
额 但是我想它受影响..比如:
declare tempid int;
declare mycursor CURSOR for select id from table where id=tempid;
如上我的tempid这个变量是需要后面取得并且赋值的 但是如果:
declare tempid int;
set tempid=2;
declare mycursor CURSOR for select id from table where id=tempid;
这样定义的话 会编译不过,那么我只能把游标的定义提前, 那么当我后面打开游标 我当然希望我的变量能影响游标的结果,比如:
declare tempid int;
declare mycursor CURSOR for select id from table where id=tempid;
set tempid=2;
open mycursor;
希望游标执行的话 里面的变量tempid=2,如果这样不行的话,那么应该怎么处理呢?
wwwwb 2014-07-03
  • 打赏
  • 举报
回复
举例说明,除非重名, 一般不会
天下如山 2014-07-03
  • 打赏
  • 举报
回复
@wwwwb into 可以的。 MySql 语法很让人蛋疼啊..... 在问个 在存储过程中 如果要使用游标 因为游标的定义不允许 别的可执行的语句在前面 那么我就会放在前面定义 但是如果我的游标中使用了变量 那么当我打开游标前 给变量赋值,当我打开游标后 这个变量会影响游标的结果么?
天下如山 2014-07-03
  • 打赏
  • 举报
回复
在存储过程中使用的 我试下你说的into.
wwwwb 2014-07-03
  • 打赏
  • 举报
回复
你是在SP中使用? declare tempid int; select id into tempid from table where tempname='需求';
天下如山 2014-07-03
  • 打赏
  • 举报
回复
求解 为啥? 难道我以后存储过程里面全用@来定义变量?
天下如山 2014-07-03
  • 打赏
  • 举报
回复
引用 1 楼 wwwwb 的回复:
变量赋值要用:=
declare定义的变量 我用:
select tempid:=id from table where tempname='需求';
这样 编译是通不过的.
wwwwb 2014-07-03
  • 打赏
  • 举报
回复
变量赋值要用:=
天下如山 2014-07-03
  • 打赏
  • 举报
回复
好的 thank you.

56,687

社区成员

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

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