BEGIN
DECLARE sqlsentences VARCHAR (8000) ;

select group_concat('select Store,`CustomerName`,`StoreName`,company,Region,`P`,`CustomerType`,CHANNEL,PROVINCE,地级市,
门店地址,`StoreLayer`,', quote(column_name), ' as type,', column_name, ' as post from post_test' separator ' union ')
into @sqlsentences
from information_schema.columns
where table_schema=database() and table_name='post_test'
and column_name not in('Store','CustomerName','StoreName','company','Region','P','CustomerType','CHANNEL',
'PROVINCE','地级市','门店地址','StoreLayer');
prepare st from @sqlsentences;
execute st;
deallocate prepare st;
END
这样不会报错,但是加上嵌套就出错,如下:
BEGIN
DECLARE sqlsentences VARCHAR (8000) ;
    select group_concat('select Store,`CustomerName`,`StoreName`,company,Region,`P`,`CustomerType`,CHANNEL,PROVINCE,地级市,
门店地址,`StoreLayer`,', quote(column_name), ' as type,', column_name, ' as post from post_test' separator ' union ')
into @sqlsentences
from information_schema.columns
where table_schema=database() and table_name='post_test'
and column_name not in('Store','CustomerName','StoreName','company','Region','P','CustomerType','CHANNEL',
'PROVINCE','地级市','门店地址','StoreLayer');        SET @sqlt=concat('select * from (\'',@sqlsentences,')',' where post>0');
prepare st from @sqlt;
execute st;
deallocate prepare st;END