我正在尝试更改表格以添加一个附加列,该列具有 org_id
的最后 3 位数字。我这样做是因为我正在使用的软件由于大小而无法读取 34000000000000000002
。我想将 34000000000000000002
转换为 002
并将其放入新列中。我认为像下面这样的东西会起作用
alter table [org] add new_org_id integer value (select right(org_id,3));
我是 sql 的新手,所以如果我还不太了解,我深表歉意。
最佳答案
你很接近。查看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/