可以将存储的列添加到Mariadb 10.7.3,臭名昭著的“错误代码1901”

发布于 2025-01-25 09:08:47 字数 4183 浏览 3 评论 0原文

我试图将生成的始终列添加到一个将两个字符解码为date的表中。

它包含一个函数(返回date),但是当表达式内衬时,它以相同的方式行为。该功能在选择中正常工作。我已经检查了我的角色集和整理以保持一致性。

sql_mode是“ error_for_for_division_by_zero,no_auto_create_user,no_engine_substitution”

这是表格架构。请注意,我有一个位置生成的列,该列工作正常。

CREATE TABLE `Photo_Gear_Date_Codes` (
  `ID` varchar(4) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL DEFAULT '....',
  `Location` varchar(253) GENERATED ALWAYS AS (case left(`ID`,2) when 'TN' then 'Tatsuno, Hyogo' else 'Unknown' end) STORED,
  `Note` varchar(1022) COLLATE latin1_general_ci DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

我定义为简化事物的函数

CREATE DEFINER=`root`@`127.0.0.1`
FUNCTION `Date_from_OM_date_code`(id3 char(1), id4 char(1))
RETURNS date
DETERMINISTIC
COMMENT 'Decode the last two digits of a four-character OM date code.'
RETURN CONVERT(
    concat(
      case ID3
        when '1' then '1971'
        when '2' then '1972'
        when '3' then '1973'
        when '4' then '1974'
        when '5' then '1975'
        when '6' then '1976'
        when '7' then '1977'
        when '8' then '1978'
        when '9' then '1979'
        when '0' then '1980'
        when 'A' then '1981'
        when 'B' then '1982'
        when 'C' then '1983'
        when 'D' then '1984'
        when 'E' then '1985'
        when 'F' then '1986'
        when 'G' then '1987'
        when 'H' then '1988'
        when 'I' then '1989'
        when 'J' then '1990'
        when 'K' then '1991'
        when 'L' then '1992'
        when 'M' then '1993'
        when 'N' then '1994'
        when 'O' then '1995'
        when 'P' then '1996'
        when 'Q' then '1997'
        when 'R' then '1998'
        when 'S' then '1999'
        when 'T' then '2000'
        when 'U' then '2001'
        when 'V' then '2002'
        when 'W' then '2003'
        when 'X' then '2004'
        when 'Y' then '2005'
        when 'Z' then '2006'
        end,
      case ID4
        when '1' then '-01-00'
        when '2' then '-02-00'
        when '3' then '-03-00'
        when '4' then '-04-00'
        when '5' then '-05-00'
        when '6' then '-06-00'
        when '7' then '-07-00'
        when '8' then '-08-00'
        when '9' then '-09-00'
        when 'A' then '-10-00'
        when 'B' then '-11-00'
        when 'C' then '-12-00'
        when 'X' then '-10-00'
        when 'Y' then '-11-00'
        when 'Z' then '-12-00'
        end), DATE);

USE `Personal`;
ALTER TABLE `Photo_Gear_Date_Codes` ADD COLUMN `Date` DATE AS 
  (Date_from_OM_date_code(SUBSTR(`ID`,3,1), RIGHT(`ID`,1))) STORED;
SELECT ID,
  `Location`,
  Date_from_OM_date_code(SUBSTR(`ID`,3,1), RIGHT(`ID`,1)) `Date`
  FROM Photo_Gear_Date_Codes

是 1901年错误代码:

函数或表达式'date_from_om_date_code()'不能在 生成的始终为的子句

错误代码1901。

的子句。但是我通过错误进行,select效果很好!

ID位置日期
S7Y未知1977-11-00
TND1TATSUNO,HYOGO,1984-01-00
TND4 TND4 TND4TND4,1984-04-00
TNGBTATSUNO,HYOGO,HYOGO ,1987-11-11-00
TNGBHOOGO,HYOGO,TNH9 TNH9 TNH9 TATSUNO ,
TNH9TATSUNO,TATSUNO,TATSUNO,TATSUNO,TATSUNO, TATSUNO,YOOGO1989-05-00
TNJ2TATSUNO,HYOGO1990-02-00
TNM6 TNM6 TNM6 TATSUNO,HYOGO,HYOGO,HYOGO, HYOGO1990-03-00
TNM6TNJ5 TNJ5 TATSUNO,HYOGO ,1990-05-00-05-00
TNM91993-06-00
TATSUNO, HYOGOTNJ3 TNJ3
TNM9 TNM9 TNM9 TATSUNO TnoaTatsuno,Hyogo,1995-10-00

,似乎应该可以工作……我将头发拉到这头!

事先感谢您指出我愚蠢的地方! :-)

I'm trying to add a GENERATED ALWAYS column to a table that will decode two characters into a DATE.

It contains a function (returning DATE), but it was misbehaving the same way when the expression was in-lined. The function works fine in a SELECT. I've checked my character set and collation for consistency.

sql_mode is "ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

Here is the table schema. Note I have a Location generated column, which is working fine.

CREATE TABLE `Photo_Gear_Date_Codes` (
  `ID` varchar(4) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL DEFAULT '....',
  `Location` varchar(253) GENERATED ALWAYS AS (case left(`ID`,2) when 'TN' then 'Tatsuno, Hyogo' else 'Unknown' end) STORED,
  `Note` varchar(1022) COLLATE latin1_general_ci DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

Here is the function I defined to try to simplify things, although I got the same error when I in-lined this code:

CREATE DEFINER=`root`@`127.0.0.1`
FUNCTION `Date_from_OM_date_code`(id3 char(1), id4 char(1))
RETURNS date
DETERMINISTIC
COMMENT 'Decode the last two digits of a four-character OM date code.'
RETURN CONVERT(
    concat(
      case ID3
        when '1' then '1971'
        when '2' then '1972'
        when '3' then '1973'
        when '4' then '1974'
        when '5' then '1975'
        when '6' then '1976'
        when '7' then '1977'
        when '8' then '1978'
        when '9' then '1979'
        when '0' then '1980'
        when 'A' then '1981'
        when 'B' then '1982'
        when 'C' then '1983'
        when 'D' then '1984'
        when 'E' then '1985'
        when 'F' then '1986'
        when 'G' then '1987'
        when 'H' then '1988'
        when 'I' then '1989'
        when 'J' then '1990'
        when 'K' then '1991'
        when 'L' then '1992'
        when 'M' then '1993'
        when 'N' then '1994'
        when 'O' then '1995'
        when 'P' then '1996'
        when 'Q' then '1997'
        when 'R' then '1998'
        when 'S' then '1999'
        when 'T' then '2000'
        when 'U' then '2001'
        when 'V' then '2002'
        when 'W' then '2003'
        when 'X' then '2004'
        when 'Y' then '2005'
        when 'Z' then '2006'
        end,
      case ID4
        when '1' then '-01-00'
        when '2' then '-02-00'
        when '3' then '-03-00'
        when '4' then '-04-00'
        when '5' then '-05-00'
        when '6' then '-06-00'
        when '7' then '-07-00'
        when '8' then '-08-00'
        when '9' then '-09-00'
        when 'A' then '-10-00'
        when 'B' then '-11-00'
        when 'C' then '-12-00'
        when 'X' then '-10-00'
        when 'Y' then '-11-00'
        when 'Z' then '-12-00'
        end), DATE);

Here is how I'm trying to add the generated column:

USE `Personal`;
ALTER TABLE `Photo_Gear_Date_Codes` ADD COLUMN `Date` DATE AS 
  (Date_from_OM_date_code(SUBSTR(`ID`,3,1), RIGHT(`ID`,1))) STORED;
SELECT ID,
  `Location`,
  Date_from_OM_date_code(SUBSTR(`ID`,3,1), RIGHT(`ID`,1)) `Date`
  FROM Photo_Gear_Date_Codes

When I execute those three statements, the second throws the 1901 error code:

Function or expression 'Date_from_OM_date_code()' cannot be used in
the GENERATED ALWAYS AS clause of Date

Error code 1901.

But I proceed through the error, and the SELECT works just fine!

IDLocationDate
S7YUnknown1977-11-00
TND1Tatsuno, Hyogo1984-01-00
TND4Tatsuno, Hyogo1984-04-00
TNGBTatsuno, Hyogo1987-11-00
TNH9Tatsuno, Hyogo1988-09-00
TNI5Tatsuno, Hyogo1989-05-00
TNJ2Tatsuno, Hyogo1990-02-00
TNJ3Tatsuno, Hyogo1990-03-00
TNJ5Tatsuno, Hyogo1990-05-00
TNM6Tatsuno, Hyogo1993-06-00
TNM9Tatsuno, Hyogo1993-09-00
TNOATatsuno, Hyogo1995-10-00

It seems like it should work… I'm pulling my hair out over this one!

Thanks in advance for pointing out where I'm being stupid! :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2025-02-01 09:08:47

正如Mariadb在生成的列上说::

此表达式可能会根据表中其他列的值生成值,或者它可能通过调用内置函数或用户定义的函数(UDFS)来生成值。

udfs是写在C ++,不在SQL中。如您所见,标准,基于SQL的用户创建的功能不包含在列表中,因此错误消息。

将功能的身体作为表达式包括为生成的列的代码。

As mariadb's documentation on generated column says:

This expression might generate the value based on the values of other columns in the table, or it might generate the value by calling built-in functions or user-defined functions (UDFs).

UDFs are functions written in c++, not in sql. As you can see, standard, sql-based user created functions are not included in the list, hence the error message.

Include your function's body as an expression as the generated column's code instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文