原创 2008年12月25日 11:54:00
今天在使用数据库临时表的游标时,发现了这个异常。
经查找资料,最终结果。这里和大家分享一下。 字符集问题还是一定要统一的才是最简单的。
create temporary table temp2(mc1 varchar(20) default ‘‘,mc2
varchar(20)default ‘‘,mc3 varchar(20)default ‘‘,mc4 varchar(20)default
‘‘,mc5 varchar(20)default ‘‘,parentid varchar(20),levelnum int); --
生成临时表
declare mycur67 CURSOR for select
concat(replace(space(levelnum-1),space(1),‘------‘),mc1,mc2,mc3,mc4,mc5)
as mc,parentid as location from temp2;
异常信息为:
Illegal mix of collations for operation ‘concat‘
将concat的代码去掉后面的部分内容,运行结果为
declare mycur67 CURSOR for select
concat(replace(space(levelnum-1),space(1),‘------‘),mc1) as mc,parentid
as location from temp2;
错误:Illegal mix of collations (utf8_general_ci,COERCIBLE) and
(gb2312_chinese_ci,IMPLICIT) for operation ‘concat‘
可见,是2个字段的编码类型不一致造成的。
原来我的数据库创建时,选择的是gb2312编码,而字段操作默认为UTF8的编码。 绝对统一使用UTF-8
create
temporary table temp2(mc1 varchar(20) default ‘‘,mc2 varchar(20)default
‘‘,mc3 varchar(20)default ‘‘,mc4 varchar(20)default ‘‘,mc5
varchar(20)default ‘‘,parentid varchar(20),levelnum int)DEFAULT
CHARSET=UTF8;
运行结果正常。
经查找资料,如下说的很清楚
mysql> show variables like
"%character%"; show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name |
Value |
+--------------------------+----------------------------+
|
character_set_client | latin1 |
| character_set_connection | latin1 |
|
character_set_database | latin1 |
| character_set_results | latin1 |
|
character_set_server | latin1 |
| character_set_system | utf8 |
|
character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set
(0.00 sec)
+----------------------+-------------------+
|
Variable_name | Value |
+----------------------+-------------------+
|
collation_connection | latin1_swedish_ci |
| collation_database |
latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
fix it with
set collation_database=utf8_general_ci;
set collation_connection=utf8_general_ci;
set
collation_server=utf8_general_ci;
原文地址:https://www.cnblogs.com/lxwphp/p/8329186.html