我的表中有882行列(StartDate,EndDate,Capacity,PitchType_Skey,Site_Skey),这些行基于2010日期,我想要做的是在2011年插入另外882行,但是我无法想象如何做到这一点,你能帮忙吗?我已经开始插入声明了:
INSERT INTO Cap1 (StartDate, EndDate, Capacity, PitchType_Skey, Site_Skey)
SELECT
Add 1 YEAR to StartDate,
Add 1 YEAR to EndDate,
Capacity,
Pitch_Type_Skey,
Site_Skey
FROM Cap1
WHERE YEAR StartDate = 2010
先谢谢
韦恩
答案 0 :(得分:3)
假设StartDate
和EndDate
属于数据类型的日期或日期时间,那么您可以使用DATEADD
函数将年份增量加1,如下所示:
INSERT INTO Cap1 (StartDate, EndDate, Capacity, PitchType_Skey, Site_Skey)
SELECT
DATEADD(yy, 1, StartDate),
DATEADD(yy, 1, EndDate),
Capacity,
Pitch_Type_Skey,
Site_Skey
FROM Cap1
WHERE YEAR(StartDate) = 2010;
请注意:正如@Bridge所述,@ brykneval YEAR
function不是sargable,因此SQL Server优化器无法在该字段上使用索引在YEAR
函数内。为了获得更好的效果,请改用谓词BETWEEN
或<=
,>=
。
答案 1 :(得分:0)
INSERT INTO Cap1 (StartDate, EndDate, Capacity, PitchType_Skey, Site_Skey)
SELECT
DateAdd(yy,1,StartDate),
DateAdd(yy, 1,EndDate),
Capacity,
Pitch_Type_Skey,
Site_Skey
FROM Cap1
WHERE YEAR(StartDate) = 2010