SQL 服务器 : Add a column with calculated Field

标签 sql sql-server ddl calculated-columns

我正在尝试更改表格以添加一个附加列,该列具有 org_id 的最后 3 位数字。我这样做是因为我正在使用的软件由于大小而无法读取 34000000000000000002。我想将 34000000000000000002 转换为 002 并将其放入新列中。我认为像下面这样的东西会起作用

alter table [org] add new_org_id integer value (select right(org_id,3));

我是 sql 的新手,所以如果我还不太了解,我深表歉意。

enter image description here

最佳答案

你很接近。查看documentation以获得正确的语法。

alter table [org] add new_org_id as right(org_id,3);

此外,您可能希望将其设为 persisted计算列

PERSISTED Specifies that the Database Engine will physically store the computed values in the table, and update the values when any other columns on which the computed column depends are updated. Marking a computed column as PERSISTED allows an index to be created on a computed column that is deterministic, but not precise. For more information, see Indexes on Computed Columns. Any computed columns used as partitioning columns of a partitioned table must be explicitly marked PERSISTED. computed_column_expression must be deterministic when PERSISTED is specified.

关于SQL 服务器 : Add a column with calculated Field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53341558/

相关文章:

sql - INSERT INTO 存储过程的输出

sql - 获取期初余额

sql - 获取 TIME(7) 的平均值

sql - 加入数据透视表sql

MySQL - 防止一行引用自身

mysql - 将 MySQL 表更新为构造的双聚合,这取决于表本身

mysql - 在 MYSQL 中 - 在列中最多选择特定值

sql-server - 如何从另一个容器连接到 SQL Server docker 容器?

hive - hive ddl 脚本的标准文件扩展名?

java - 如何自定义使用 Hibernate 的 hbm2ddl(使用注释)生成的 DDL?