我知道预处理sql是可以通过 '?' 来传入参数,但是怎么传出参数呢我有这样的一个需求,预处理都是放存储过程中处理的:--tableName是可变的,所以我想用预处理sql语句来处理
set @tableName='';
select get_table_name(param1) into @tableName;--我的问题就是下面 @outParam 怎么样才能在预处理sql语句中作为参数传出来
set @outParam=0;set @strSql = concat('select c1 into @outParam from',@tableName);prepare st from @strSql;execute st;deallocate prepare st;谢谢了

解决方案 »

  1.   

    可能预处理语句当中还有类似 
    set @outParam=0;
    set @strSql = concat('select  @outParam:=c1 from',@tableName);都是要求带输出参数的需求,都没找到法子啊,目前自己在通过case 在分表处理,问题是现在tableName种类增加了我就没办法了,所以就想 prepare处理的
      

  2.   

    执行完预备语句后,用户变量@outParam就已经被赋过值了,直接SET outParam=@outParam就可以了:
    DELIMITER $$
    CREATE PROCEDURE getParam(OUT outParam VARCHAR(50))
    BEGIN
    ...
    set @tableName='';
    select get_table_name(param1) into @tableName;
    set @outParam=0;
    set @strSql = concat('select c1 into @outParam from',@tableName);
    prepare st from @strSql;
    execute st;
    deallocate prepare st;
    SET outParam=@outParam;
    ...
    END $$
    DELIMITER ;mysql>CALL getParam(@outParam);
    mysql>SELECT @outParam;