在函数case和VARCHAR上获取MySQL语法错误

时间:2017-11-24 20:05:00

标签: java mysql jboss db2

当我尝试运行简单的MySQL函数创建查询时,我收到一些SQL语法错误。这些正在研究Db2。我想在MySQL上运行它。

CREATE FUNCTION SAMPLE1
  (
    I INTEGER
  ) RETURNS VARCHAR(4)
  BEGIN
    CASE
      WHEN I < 256 THEN RETURN CAST(SUBSTR(HEX(I), 1, 2) AS VARCHAR(4));
      WHEN I > 255 AND I < 4096 THEN RETURN CAST((SUBSTR(HEX(BITAND((I / 256), 255)), 2, 1) || SUBSTR(HEX(BITAND(I, 255)), 1, 2)) AS VARCHAR(4));
      ELSE
        RETURN CAST((SUBSTR(HEX(BITAND((I / 256), 255)), 1, 2) || SUBSTR(HEX(BITAND(I, 255)), 1, 2)) AS VARCHAR(4));
    END CASE;
  END;

在mysql上运行后,我收到错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(4));
      WHEN I > 255 AND I < 4096 THEN RETURN CAST((SUBSTR(HEX(BITAND' at line 11

和下一个

  CREATE FUNCTION SAMPLE2
  (
    PREFIX VARCHAR(43),
    IP4 BIGINT,
    VERSION INTEGER
  ) RETURNS VARCHAR(39)
  BEGIN
    RETURN UPPER(PREFIX ||
      (CASE WHEN VERSION = 4 THEN
                      CAST (BITAND(((IP4 + 4294967296) / 16777216), 255) AS VARCHAR(3)) || '.' ||
                      CAST (BITAND(((IP4 + 2147483648) / 65536), 255) AS VARCHAR(3)) || '.' ||
                      CAST (BITAND(((IP4 + 2147483648) / 256), 255) AS VARCHAR(3)) || '.' ||
                      CAST (BITAND((IP4 + 2147483648), 255) AS VARCHAR(3))
      ELSE
                      SUPERHEX(BITAND(((IP4 + 4294967296) / 65536), 65535)) || ':' ||
                      SUPERHEX(BITAND(IP4, 65535))
      END));
  END;

并收到错误

you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(3)) || '.' ||
                      CAST (BITAND(((IP4 + 2147483648) / 65536), 255) AS ' at line 14

和最后一个查询

CREATE FUNCTION SAMPLE3
  (
    CONNECTED TIMESTAMP,
    DISCONNECTED TIMESTAMP,
    INTERVAL BIGINT
  ) RETURNS INTEGER
  DETERMINISTIC
  NO EXTERNAL ACTION
  CONTAINS SQL
  CALLED ON NULL INPUT
  BEGIN ATOMIC
    IF (CONNECTED IS NULL) THEN
        RETURN -1;
    ELSEIF (DISCONNECTED IS NULL) THEN
        RETURN CASE WHEN (INTERVAL = 0 OR CONNECTED + INTERVAL MINUTES > CURRENT TIMESTAMP) THEN 1 ELSE 0 END;
    ELSE
        RETURN CASE WHEN (INTERVAL = 0 OR CONNECTED + INTERVAL MINUTES > CURRENT TIMESTAMP) AND CONNECTED > DISCONNECTED THEN 1 ELSE 0 END;
    END IF;
  END;

收到错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERVAL BIGINT
  ) RETURNS INTEGER
  DETERMINISTIC
  NO EXTERNAL ACTION
  CONTA' at line 9
  1. mysql VARCHAR函数中没有使用CAST吗?
  2. INTERVAL BIGINTCALLED ON NULL INPUTCASE WHEN THEN ELSE END在mysql查询中有所不同?
  3. 有任何建议或解决方案吗?

2 个答案:

答案 0 :(得分:0)

我认为它不像这样

&#34;当我&lt; 256 THEN RETURN CAST(SUBSTR(HEX(I),1,2)AS VARCHAR(4))&#34;

就像这样

当我&lt; 256 THEN RETURN CAST(SUBSTR(HEX(I),1,2)AS VARCHAR(4)

答案 1 :(得分:0)

MySQL的CAST使用/接受的类型非常通用。尝试用CAST(.... AS VARCHAR(X))替换所有CAST(... AS CHAR)。如果需要强制使用精确类型,可以将转换结果分配给声明的变量。

编辑:进一步测试; CAST(... AS CHAR(X))也有效;它是演员MySQL的VAR部分显然不喜欢。