首页 > 解决方案 > 将生成的列添加到现有表 Postgres

问题描述

我正在尝试使用此脚本将生成的列添加到现有表中。

alter table Asset_Store add column

md5_hash VARCHAR(100) GENERATED ALWAYS AS 

(CAST(UPPER(    
        case
             when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
             when Asset_ID is not null then MD5(Asset_ID)
             else null
        end 
) as VARCHAR(100)))

STORED

;

但我收到一个错误:

SQL Error [42601]: ERROR: syntax error at or near "("
 Position: 88
 ERROR: syntax error at or near "("
 Position: 88
 ERROR: syntax error at or near "("
 Position: 88

问题是什么?我不明白。

在我的 Asset_Store 表的架构中,列
OR_ID 是int,Asset_ID 是varchar(100)

我想它需要一个稍微不同的语法......但什么是正确的语法?

标签: sqlpostgresql

解决方案


你的语法是正确的。您的 PostgreSQL 版本显然不是。

在版本 12 中:

create table asset_store(or_id text, asset_id text);

alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS 
(CAST(UPPER(    
        case
             when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
             when Asset_ID is not null then MD5(Asset_ID)
             else null
        end 
) as VARCHAR(100)))
STORED
;
ALTER TABLE
Time: 17.678 ms

推荐阅读