我想创建一个表。这是创建表脚本
CREATE TABLE [dbo].[DimTenure1]
(
[TenureWeekID] [int] IDENTITY(1,1) NOT NULL,
[TenureWeek] [smallint] NOT NULL,
[TenureWeekBand] ,
[TenureMonth] [smallint] NOT NULL,
[TenureMonthBand] ,
)
我必须创建TenureWeekBand
和TenureMonthBand
动态。
例如,当用户在TenureWeek
中输入1时,会在TenureWeekBand
中自动计算出来。
我可以在TenureWeekBand
和TenureMonthBand
列的创建表脚本中定义什么?
表
TenureWeek TenureWeek TenureMonth [TenureMonthBand]
1 week of 1 enrollment 1 month of 1 enrollment
2 week of 2 enrollment 1 month of 1 enrollment
3 week of 3 enrollment 1 month of 1 enrollment
4 week of 4 enrollment 1 month of 1 enrollment
5 week of 5 enrollment 2 month of 2 enrollment
6 week of 6 enrollment 2 month of 2 enrollment
答案 0 :(得分:0)
您可以使用computed column:
CREATE TABLE [dbo].[DimTenure1]
(
[TenureWeekID] [int] IDENTITY(1,1) NOT NULL,
[TenureWeek] [smallint] NOT NULL,
[TenureWeekBand] AS ('week of ' + CAST(TenureWeek AS VARCHAR(2)) + ' enrollment') PERSISTED,
[TenureMonth] [smallint] NOT NULL,
[TenureMonthBand] AS ('month of ' + CAST(Tenuremonth AS VARCHAR(2)) + ' enrollment') PERSISTED,
)
这可能不会让人怀疑是否真的有必要,因为它可以很容易地添加到视图(或报告)中,从而节省存储空间并加快插入/更新数据的过程你的桌子。