存储过程内写了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;

解决方案 »

  1.   

    select * from ViewTb1 where pos < 7
    插入到1个临时表中保存不行?
      

  2.   

    这个viewtb1已经是第二层临时表了再临时下去会疯的
      

  3.   

    这里说的返回第2次指的是 不要返回select count(1) ...的结果集
      

  4.   

    应该是这样吧 ?
      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; 没有分号语法通过吗 ?
      

  5.   

    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;
      

  6.   

    不好意思我复制的时候删了一部分where,实际sql都有分号。
    目前在客户端会报1312错误,差不多意思是说我返回了多个数据集,这里唯一可能产生多个数据集的就是它把第一句select count也算成一个数据集了
      

  7.   

    我在mysql workbench下是正确的,程序调用时候报
    Error 1312: PROCEDURE xxxx can't return a result set in the given context
      

  8.   

    ViewTb1 ViewTb2 是不是有相同的列?
      

  9.   

    你用了into 语句,我怎么应该只返回一个数据集。测试过删除
    select count(1) from information_schema.tables where table_name = 'ViewTb1' into @cnt;
    就不报错了吗;
      

  10.   

    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;
      

  11.   

    看看,到底是不是count一句造成的错误。如果是,可以考虑使用set语句来避免返回记录集。
    set @cnt = (select count(1) from information_schema.tables where table_name = 'ViewTb1');
      

  12.   

    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;
      

  13.   

    谢谢楼上各位,这几天有事不能测试,等过两天再回CSDN。
      

  14.   

    [SQL]select count(1) from test1 into @cnt;
    受影响的行: 1
    时间: 0.000s
    我这里就没返回啊,只返回影响行数。