1

我有下表:使用 SQL Server

create table PoojaDetails
(
    PoojaDetailsID int identity constraint pk_PoojaDetailsID Primary Key, 
    ReceiptNo AS 'PB' + '/' + datepart(yy,getdate()) + '/' + RIGHT('000000' + CAST(PoojaDetailsID AS VARCHAR(10)), 6) PERSISTED,
    ReceiptDate datetime not null constraint df_ReceiptDate default getdate(),
    FirstName varchar(100) not null,
    LastName varchar(100) not null,
    TelNo bigint
)

当我执行查询时:表 'PoojaDetails' 中的计算列 'ReceiptNo' 无法持久化,因为该列是不确定的。

我想将 ReceiptNo 设为:PB/year/no 例如;PB/13/213433

有任何想法吗???谢谢你的时间

4

2 回答 2

1

如果您可以依赖ReceiptDate始终由其默认约束填充并且永不更改,那么您可以使用它而不是getdate()在计算列中:

create table PoojaDetails
(
    PoojaDetailsID int identity constraint pk_PoojaDetailsID Primary Key, 
    ReceiptNo AS 'PB' + '/' + CONVERT(varchar(5),datepart(yy,ReceiptDate)) + '/' + RIGHT('000000' + CAST(PoojaDetailsID AS VARCHAR(10)), 6) PERSISTED,
    ReceiptDate datetime not null constraint df_ReceiptDate default getdate(),
    FirstName varchar(100) not null,
    LastName varchar(100) not null,
    TelNo bigint
)
于 2013-11-12T09:06:49.917 回答
1

请试一试,

create table PoojaDetails
(
    PoojaDetailsID int identity constraint pk_PoojaDetailsID  Primary Key, 
    ReceiptNo  AS 'PB' + '/' + cast(datepart(yy,getdate()) as varchar(25)) + '/' + RIGHT('000000' + CAST(PoojaDetailsID AS VARCHAR(10)), 6) ,
    ReceiptDate datetime not null constraint df_ReceiptDate default getdate(),
    FirstName varchar(100) not null,
    LastName varchar(100) not null,
    TelNo bigint
)
于 2013-11-12T09:23:40.330 回答