用于插入周和月的动态sql查询

时间:2014-11-12 16:00:32

标签: sql

我想创建一个表。这是创建表脚本

CREATE TABLE [dbo].[DimTenure1]
(
   [TenureWeekID] [int] IDENTITY(1,1) NOT NULL,
   [TenureWeek] [smallint] NOT NULL,
   [TenureWeekBand] ,
   [TenureMonth] [smallint] NOT NULL,
   [TenureMonthBand] ,
)

我必须创建TenureWeekBandTenureMonthBand动态。

例如,当用户在TenureWeek中输入1时,会在TenureWeekBand中自动计算出来。

我可以在TenureWeekBandTenureMonthBand列的创建表脚本中定义什么?

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

1 个答案:

答案 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,
)

这可能不会让人怀疑是否真的有必要,因为它可以很容易地添加到视图(或报告)中,从而节省存储空间并加快插入/更新数据的过程你的桌子。