MySQL存储过程需要比较具有不同整理顺序的2列

时间:2016-05-19 19:18:53

标签: mysql stored-procedures unicode where-clause collation

我有一个像这样的MySql存储过程

CREATE DEFINER=`uwcrossword`@`%` PROCEDURE `sp_UpdCodePage`(IN in_id int, 
IN nm varchar(25), IN nis varchar(25), IN lang varchar(25), 
IN stpt varchar(5), IN endpt varchar(5))
BEGIN
UPDATE `uwcrossword`.`CODEPAGE`
SET
`ID` = in_id,
`Name` = nm,
`NameInScript` = nis,
`Language` = lang,
`StartPoint` = stpt,
`EndPoint` = endpt
WHERE [`ID` = in_id COLLATE utf8-general-ci] 
AND [Language = lang COLLATE utf8-unicode-ci];
END

ID列没有特定的排序规则。 Language列已整理utf8-unicode-ci

我得到的错误

MySQL说:

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'[`ID` = in_id COLLATE utf8-general-ci]和[语言= lang COLLATE utf8-unicode'第11行附近使用正确的语法

当我尝试运行相同而没有任何整理信息时

CREATE DEFINER=`uwcrossword`@`%` PROCEDURE `sp_UpdCodePage`(IN in_id int, 
IN nm varchar(25), IN nis varchar(25), IN lang varchar(25), 
IN stpt varchar(5), IN endpt varchar(5))
BEGIN
UPDATE `uwcrossword`.`CODEPAGE`
SET
`ID` = in_id,
`Name` = nm,
`NameInScript` = nis,
`Language` = lang,
`StartPoint` = stpt,
`EndPoint` = endpt
WHERE [`ID` = in_id] 
AND [Language = lang];
END
MySQL说:

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'[`ID` = in_id]附近使用正确的语法       和[语言= lang]'在第13行

1 个答案:

答案 0 :(得分:0)

正如Expression Syntax所述, simple_expr 语法制作可以产生(expr [, expr] ...)。请注意,括号是圆形的,而不是方形的。

但是,在这种情况下,不需要括号:

CREATE DEFINER=`uwcrossword`@`%` PROCEDURE sp_UpdCodePage(
  IN in_id INT,
  IN nm    VARCHAR(25),
  IN nis   VARCHAR(25),
  IN lang  VARCHAR(25), 
  IN stpt  VARCHAR(5),
  IN endpt VARCHAR(5)
)

  UPDATE uwcrossword.CODEPAGE
  SET    ID           = in_id,
         Name         = nm,
         NameInScript = nis,
         Language     = lang,
         StartPoint   = stpt,
         EndPoint     = endpt
  WHERE  ID           = in_id COLLATE utf8-general-ci
     AND Language     = lang  COLLATE utf8-unicode-ci

;