mysql存储过程 把SQL语句返回结果赋给一个变量,该SQL语句返回的结果不止一条,该怎么写,新手,求指教!

BEGIN
declare a int default 1;
set a=x;
select userid into @b from userinfo where deptid=a;
select userid,salary from salaryinfo where userid=@b;
END
存储过程如上,第一条SQL语句查询出的结果不止一条 应该怎么改?求大神指教!

第1个回答  2012-01-09
在机器上装好sqlserver2005和mysql的驱动,sqlserver2005中在要导出的数据库上点右键,有个导出数据,跟着步骤往下走就行了,期间很可能遇到数据类型转换的问题,快到最后一步时把包保存下来,报错的话,可以把包打开编辑一下。
如果数据量小的话,导入excel里再导到mysql也好
第2个回答  推荐于2018-04-20
你可以建一个临时表,吧这些数据全存进去
用这条语句
select into本回答被网友采纳
第3个回答  2011-12-29
两条合成一条sql:
select A.userid , B.salary from userinfo A inner join salaryinfo B on A.userid=B.userid where A.deptid=a追问

额 我就是把一条拆两天看看效果的...

追答

BEGIN
declare a int default 1;
set a=x;
declare @b table(userid int);
insert into @b select userid from userinfo where deptid=a;
select userid ,salary from eWL_Role where userid in (select * from @b);
END

这个怎么样,满意不?

追问

额 提示说
declare @b table(userid int);
insert into @b select userid from userinfo where deptid=a;附近有语法错误...

追答

select userid ,salary from salaryinfo where userid in (select * from @b);

本回答被提问者采纳
第4个回答  2011-12-29
你可以运用java的List接口动态存取你要输出的参数,这不仅只是数据库语句,要用到java追问

额 存储过程要用java吗?

追答

我以为你在制作jsp呢!

第5个回答  2011-12-29
select userid,salary from salaryinfo where userid in
(select userid from userinfo where deptid=@a)

或:
select userid,salary from salaryinfo where exists(
select userid from userinfo where deptid=@a and userinfo.userid=salaryinfo.userid)

推荐第二种,因为exists系统会根据索引进行搜索
相似回答