set @state2=
      case
when contractCategories='0' then ''
when contractCategories='1' then ' and experimentGroupID is not null'
when contractCategories='2' then concat(' and  groupid in(select groupID from contractgroup where description=','研发类)')---出现错误
                when contractCategories='3' then concat(' and  groupid in(select groupID from contractgroup where description=','贸易类)')
  end;
我写的存储过程中,当执行第二行就会出现错误,请问MySQL中有没有像sql server中的convert()函数一样的转换函数。如果没有,我该怎么做。

解决方案 »

  1.   

    有,CAST、CONVERT
    CAST(expr AS type), CONVERT(expr,type), CONVERT(expr USING transcoding_name) The CAST() and CONVERT() functions take a value of one type and produce a value of another type. The type can be one of the following values: BINARY[(N)] CHAR[(N)] DATE DATETIME DECIMAL SIGNED [INTEGER] TIME UNSIGNED [INTEGER] 
      

  2.   

    set @state2=改成declare state2 varchar(200);
    set state2=case when contractCategories=.....把这个@去掉。
      

  3.   

    你在SP中,加入
    SELECT @state2
    看看结果,
    第二次运行时,再看看结果
      

  4.   

    mysql> delimiter //
    mysql>
    mysql> CREATE PROCEDURE simpleproc (IN contractCategories char(1))
        -> BEGIN
        ->   declare state2 varchar(100);
        ->  set state2=
        ->  case
        ->          when contractCategories='0' then ''
        ->          when contractCategories='1' then ' and experimentGroupID is notnull'
        ->          when contractCategories='2' then concat(' and  groupid in(select groupID from contractgroup where description=','研发类)')
        ->          when contractCategories='3' then concat(' and  groupid in(select groupID from contractgroup where description=','贸易类)')
        ->  end;
        ->
        ->   SELECT state2;
        -> END;
        -> //
    Query OK, 0 rows affected (0.06 sec)mysql>
    mysql> delimiter ;
    mysql>
    mysql> CALL simpleproc('2');
    +------------------------------------------------------------------------------+| state2                                                                       |+------------------------------------------------------------------------------+|  and  groupid in(select groupID from contractgroup where description=研发类) |+------------------------------------------------------------------------------+1 row in set (0.00 sec)Query OK, 0 rows affected (0.00 sec)mysql>
      

  5.   

    如果还有问题,则检查一下你当前的字符集,
    你可以 set names 'bg2312' 试一下。如果可以则在mysql的 my.ini文件中改一下 character_set_server='utf8' 或者gb2312
      

  6.   

    注意description,应该是字符型吧
    set @state2=
        case
    when @contractCategories='0' then ''
    when @contractCategories='1' then ' and experimentGroupID is not null'
    when @contractCategories='2' then concat(' and  groupid in(select groupID from contractgroup where description=\'','研发类\')')
    when @contractCategories='3' then concat(' and  groupid in(select groupID from contractgroup where description=\'','贸易类\')')
    end;
      

  7.   

    @contractCategories='0'
    @contractCategories='1'
    运行时都有结果,但是到@contractCategories='2'就没有结果了,弹出一个乱码的对话框
      

  8.   

    呵呵,字符集问题
    show variables like 'char%'; 
    贴结果
      

  9.   

    谢谢楼上的热心,但是我是刚刚接触MySQL,不明白你说的意思,我应该怎么做,能说的详细一些吗?
      

  10.   

    进入MYSQL的SHELL OR 在SQLYOG等待图形化管理工具中PSATE上述代码,看看
    结果 
    OR
    检查一下 MY.INI中
    [client]default-character-set=utf8
    [mysql]default-character-set=utf8
      

  11.   

    始终不太明白楼主的问题是什么?是建存储过程的时候报错?还是执行存储过程的时候报错? 错误信息是什么?
    你可以到MySQL的数据文件目录下找到 xxx.err 看一下里面的错误信息。 这样也容易让大家了解你的问题和模拟你的问题。mysql>
    mysql> delimiter //
    mysql>
    mysql> CREATE PROCEDURE `simpleproc`()
        -> BEGIN
        ->         set @state2=
        ->         case
        ->                 when @contractCategories='0' then ''
        ->                 when @contractCategories='1' then ' and experimentGroupID is notnull'
        ->                 when @contractCategories='2' then concat(' and  groupid in(select groupID from contractgroup where description=','研发类)')
        ->                 when @contractCategories='3' then concat(' and  groupid in(select groupID from contractgroup where description=','贸易类)')
        ->         end;
        ->
        -> END//
    Query OK, 0 rows affected (0.00 sec)mysql>
    mysql> delimiter ;
    mysql>
    mysql> set @contractCategories='1';
    Query OK, 0 rows affected (0.00 sec)mysql> call simpleproc();
    Query OK, 0 rows affected (0.00 sec)mysql>
    mysql> select @state2
        -> ;
    +-----------------------------------+
    | @state2                           |
    +-----------------------------------+
    |  and experimentGroupID is notnull |
    +-----------------------------------+
    1 row in set (0.00 sec)mysql> set @contractCategories='2';
    Query OK, 0 rows affected (0.00 sec)mysql> call simpleproc();
    Query OK, 0 rows affected (0.00 sec)mysql>
    mysql> select @state2;
    +------------------------------------------------------------------------------+| @state2                                                                      |+------------------------------------------------------------------------------+|  and  groupid in(select groupID from contractgroup where description=研发类) |+------------------------------------------------------------------------------+1 row in set (0.00 sec)mysql>
      

  12.   

    谢谢大家,我已经解决了,
    when contractCategories='2' then concat(' and  groupid in(select groupID from contractgroup where description=',char(39),'贸易类',char(39),')')
    用了ASCII码表的方式。感谢大家热心。
      

  13.   

    呵呵,已经说过,应该是字符型吧 ,用\转义 OR CHR(39)均可
      

  14.   

    用concat()中用char(39)的话,得到的数据中就会有'',如要输出3的话,用该方法得到的就是'3'了,不是吗