我想将 SQL Server 中的现有列更改为计算并保留而不删除表/列。
我有一个自动递增的ID
列和另一个ReportID
用这个计算格式化的列:
ReportID = 'RPT'+{FiscalYear}+{0s}+{Auto Incremented ID}
例如:
- 对于 ID = 1, 2, ...., 10, 11, ..., 100, 101
- ReportID 将是
RPT2122000001
,RPT2122000002
,...,RPT2122000010
,RPT2122000011
, ...,RPT2122000101
,RPT2122000102
以前,我们在“插入后”触发器中执行此操作 - 计算值并更新行。但是通过这样做,当负载很高并且报告由不同的用户并行生成时,一些 ReportID 会被复制。
因此,为了解决这个问题,我想将现有列更改为计算列,'RPT'+{FiscalYear}+{0s}+{Auto Incremented ID}
但问题是我希望现有数据保持不变。因为如果现在运行计算,所有上一年的数据都将被修改为当前财政年度,这是错误的。
当我通过在 Management Studio 中设置计算值直接尝试时,它也在内部运行 drop 并在后台重新添加。
我看到了很多答案,但他们还不够满意。
我尝试使用计算值创建一个新列,然后尝试重命名,这也是不允许的。
编辑 1
错误:
Computed column 'ReportID' in table 'Tmp_wp_tra_report_creation' cannot be persisted because the column is non-deterministic
编辑 2
财政年度计算:
(select (case when ((select top 1 a.Pc_FromDate from wp_pc_calendar a where a.Pc_FromDate>=getdate())<=getdate()) then (select top 1 b.Pc_FinYear from wp_pc_calendar b where b.Pc_FromDate>=getdate() order by b.Pc_FromDate asc ) else (case when ((select top 1 c.Pc_FromDate from wp_pc_calendar c where c.Pc_FromDate<=getdate() )<=getdate()) then (select top 1 d.Pc_FinYear from wp_pc_calendar d where d.Pc_FromDate<=getdate() order by d.Pc_FromDate desc ) else 'No Records' end) end) as finyear)
另外,有没有不创建新列的方法?