是否可以将字符串连接到序列值并用作列默认值?
Is it possible to concatenate a string to a sequence value and use as a column default value?
我想为序列中的列设置一个默认值,就像所做的一样 here,但也在序列前面添加一个值,以便保存在 [=14= 中的值] 看起来像 P123
。这可能吗?
完全有可能。
将 post 中的示例更改为这样的内容:
create sequence mainseq as bigint start with 1 increment by 1;
create table mytable (
id varchar(20) not null constraint DF_mytblid default 'p' + CAST(next value for mainseq as varchar(10)),
code varchar(20) not null
)
测试:
INSERT INTO MyTable (Code) VALUES ('asdf'), ('cvnb')
SELECT *
FROM MyTable
结果:
id code
p1 asdf
p2 cvnb
我想为序列中的列设置一个默认值,就像所做的一样 here,但也在序列前面添加一个值,以便保存在 [=14= 中的值] 看起来像 P123
。这可能吗?
完全有可能。
将 post 中的示例更改为这样的内容:
create sequence mainseq as bigint start with 1 increment by 1;
create table mytable (
id varchar(20) not null constraint DF_mytblid default 'p' + CAST(next value for mainseq as varchar(10)),
code varchar(20) not null
)
测试:
INSERT INTO MyTable (Code) VALUES ('asdf'), ('cvnb')
SELECT *
FROM MyTable
结果:
id code
p1 asdf
p2 cvnb