各位大牛,请帮忙看一下如下SQL,第1个Union ALL去掉,然后自动执行concat出来的结果,怎么实现?贴出存储过程的脚本,谢谢!

解决方案 »

  1.   

    需要写2个语句,顺序执行即可。第一句是定义你的语句,第二句是执行。样例代码如下:
    PREPARE stmt1 FROM 'select t.* from table_name t'; 
    EXECUTE stmt1  ; 如果需要传入参数,样例如下,问号是变量的位置,定义的变量和问号的数量要相等。按照先后顺序,问号和变量要对应。样例代码如下:
    PREPARE stmt1 FROM 'select t.* from table_name t where t.id = ? and t.name = ?'; 
    SET @a = 3; 
    SET @b = 4; 
    EXECUTE stmt1 USING @a, @b; 
      

  2.   


    HI,AHUA1001
    SELECT CONCAT('union all  select  ''', TABLE_SCHEMA ,''' as db ,''',TABLE_NAME,''' as tbname, count(1) as ro ', TABLE_SCHEMA ,'.',TABLE_NAME )  AS SqlExe  FROM information_schema.TABLES AS t  WHERE t.TABLE_TYPE = 'BASE TABLE' AND t.TABLE_SCHEMA = 'test'
    这是SQL语句,查出来的结果是
    +---------------------------------------------------------------------------------------+
    | SqlExe                                                                                |
    +---------------------------------------------------------------------------------------+
    | union all  select  'test' as db ,'accesslog' as tbname, count(1) as ro test.accesslog |
    | union all  select  'test' as db ,'t' as tbname, count(1) as ro test.t                 |
    | union all  select  'test' as db ,'t1' as tbname, count(1) as ro test.t1               |
    | union all  select  'test' as db ,'t2' as tbname, count(1) as ro test.t2               |
    | union all  select  'test' as db ,'temp' as tbname, count(1) as ro test.temp           |
    | union all  select  'test' as db ,'tn' as tbname, count(1) as ro test.tn               |
    | union all  select  'test' as db ,'tt' as tbname, count(1) as ro test.tt               |
    +---------------------------------------------------------------------------------------+
    7 rows in set (0.00 sec)
    现在需求是,将查出来的结果(如上)第1个union all去掉,单独执行后面的内容,如下,请帮忙,谢谢!
    select  'test' as db ,'accesslog' as tbname, count(1) as ro test.accesslog 
    union all  select  'test' as db ,'t' as tbname, count(1) as ro test.t                
    union all  select  'test' as db ,'t1' as tbname, count(1) as ro test.t1               
    union all  select  'test' as db ,'t2' as tbname, count(1) as ro test.t2               
    union all  select  'test' as db ,'temp' as tbname, count(1) as ro test.temp           
    union all  select  'test' as db ,'tn' as tbname, count(1) as ro test.tn               
    union all  select  'test' as db ,'tt' as tbname, count(1) as ro test.tt 
      

  3.   

    这个简单,您要的结果是一大堆的union all对吧。
    就是一个select查询出来的。
    以下,"null,null……"里的null数量,要和您的那个大查询语句里的列数保持一致。select null,null…… from dual where 1=2 union all
    这里写你的那一大堆语句。思路和您相反,您是想去掉最前的union all,我是在最前的union all加上一个东西,并且加的这个东西where 1=2,不会显示。另外感觉您的这个返回结果,有问题,可能执行不过去,你调试一下吧,有问题再回复。