Tsql SQL Server 2012创建一个循环超过500个现有列的更新

Tsql SQL Server 2012创建一个循环超过500个现有列的更新,tsql,sql-server-2012,Tsql,Sql Server 2012,我希望使用现有值加上5年,用空值更新日期列,PK是IdentityCourseID 这就是我努力实现的目标: UPDATE IdentityCourses SET Expiry = DATEADD(year, 5, IdentityCourses.DateAttained) WHERE IdentityCourseID = (SELECT IdentityCourseID FROM IdentityCourses

我希望使用现有值加上5年,用空值更新日期列,PK是
IdentityCourseID

这就是我努力实现的目标:

UPDATE IdentityCourses
SET Expiry = DATEADD(year, 5, IdentityCourses.DateAttained)
WHERE IdentityCourseID = (SELECT IdentityCourseID
                          FROM IdentityCourses 
                          INNER JOIN UnitIdentities ON  IdentityCourses.IdentityID = UnitIdentities.IdentityID
                          WHERE (IdentityCourses.CourseID = 1041) 
                            AND (UnitIdentities.IsActiveMember = 1) 
                            AND (UnitIdentities.EndDate IS NULL) 
                            AND (IdentityCourses.Expiry IS NULL) )

我认为这会对你有用:

UPDATE ic
SET Expiry = DATEADD(year, 5, ic.DateAttained)
FROM IdentityCourses AS ic
INNER JOIN UnitIdentities AS ui
  ON ic.IdentityID = ui.IdentityID
WHERE ic.CourseID = 1041
  AND ui.IsActiveMember = 1
  AND ui.EndDate IS NULL
  AND ic.Expiry IS NULL

听起来您需要使用“updatefrom”语法。有关示例,请参见此问题: