怎样让存储过程只返回1个结果集

dahuatttt 2014-01-08 02:09:43
存储过程内写了2次select,只想返回第二次select的结果集


select count(1) from information_schema.tables where table_name = 'ViewTb1' into @cnt;
if @cnt = 0 then
select * from ViewTb2 where pos < 7
else
select * from ViewTb1 where pos < 7
end if;

...全文
363 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
dahuatttt 2014-01-16
  • 打赏
  • 举报
回复
谢谢楼上各位,这几天有事不能测试,等过两天再回CSDN。
风吹腚腚凉 2014-01-16
  • 打赏
  • 举报
回复
引用 15 楼 dahuatttt 的回复:
谢谢楼上各位,这几天有事不能测试,等过两天再回CSDN。
[SQL]select count(1) from test1 into @cnt; 受影响的行: 1 时间: 0.000s 我这里就没返回啊,只返回影响行数。
Rotel-刘志东 2014-01-14
  • 打赏
  • 举报
回复
select count(1) from information_schema.tables where tablename='viewtb1' into @cnt; if @cnt=0 then select * from viewtb2 where pos<7 limit 0 else select * from viewtb1 where pos<7 end if;
ermuzi 2014-01-14
  • 打赏
  • 举报
回复
看看,到底是不是count一句造成的错误。如果是,可以考虑使用set语句来避免返回记录集。 set @cnt = (select count(1) from information_schema.tables where table_name = 'ViewTb1');
懒得去死 2014-01-14
  • 打赏
  • 举报
回复
select count(1) from information_schema.tables where table_name = 'ViewTb1' into @cnt;
if @cnt = 0 then 
    select * from ViewTb2 where pos < 7 limit 0;
else
    select * from ViewTb1 where pos < 7;
end if;
十一路 2014-01-13
  • 打赏
  • 举报
回复
你用了into 语句,我怎么应该只返回一个数据集。 测试过删除 select count(1) from information_schema.tables where table_name = 'ViewTb1' into @cnt; 就不报错了吗;
知道就是你 2014-01-09
  • 打赏
  • 举报
回复
麻烦将完成的存储过程和程序调用代码,数据库表 共享一下,如果方便的话,不然没有办法调测,只能凭空猜测.........
dahuatttt 2014-01-09
  • 打赏
  • 举报
回复
引用 9 楼 ermuzi 的回复:
ViewTb1 ViewTb2 是不是有相同的列?
是的,完全一样。
ermuzi 2014-01-09
  • 打赏
  • 举报
回复
ViewTb1 ViewTb2 是不是有相同的列?
dahuatttt 2014-01-08
  • 打赏
  • 举报
回复
我在mysql workbench下是正确的,程序调用时候报 Error 1312: PROCEDURE xxxx can't return a result set in the given context
dahuatttt 2014-01-08
  • 打赏
  • 举报
回复
引用 5 楼 JenMinZhang 的回复:
select count(*) into @cnt from information_schema.tables where table_name = 'ViewTb1' ; if @cnt > 0 then select * from ViewTb1 where pos < 7 ; else select * from ViewTb2 where pos < 7 ; end if;
不好意思我复制的时候删了一部分where,实际sql都有分号。 目前在客户端会报1312错误,差不多意思是说我返回了多个数据集,这里唯一可能产生多个数据集的就是它把第一句select count也算成一个数据集了
知道就是你 2014-01-08
  • 打赏
  • 举报
回复
select count(*) into @cnt from information_schema.tables where table_name = 'ViewTb1' ; if @cnt > 0 then select * from ViewTb1 where pos < 7 ; else select * from ViewTb2 where pos < 7 ; end if;
知道就是你 2014-01-08
  • 打赏
  • 举报
回复
应该是这样吧 ? select count(*) into @cnt from information_schema.tables where table_name = 'ViewTb1' ; if @cnt = 0 then select * from ViewTb2 where pos < 7 ; else select * from ViewTb1 where pos < 7 ; end if; 没有分号语法通过吗 ?
dahuatttt 2014-01-08
  • 打赏
  • 举报
回复
这里说的返回第2次指的是 不要返回select count(1) ...的结果集
dahuatttt 2014-01-08
  • 打赏
  • 举报
回复
引用 1 楼 wwwwb 的回复:
select * from ViewTb1 where pos < 7 插入到1个临时表中保存不行?
这个viewtb1已经是第二层临时表了。。。再临时下去会疯的
wwwwb 2014-01-08
  • 打赏
  • 举报
回复
select * from ViewTb1 where pos < 7 插入到1个临时表中保存不行?

56,687

社区成员

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

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