我要寻找的最终结果是要有一个查询,该查询将在表1中找到特定的记录,使用加入Table2中的字段检索一周的一天,使用公式来确定该日的下一个日历日期,并使用该日历日期更新Talbe1中的日期字段。
单独使用,这些碎片并不那么复杂。 但是,将这些碎片放在一起是一个挑战。如果有人可以给我查询的结构,我将非常感谢它。 通过“结构”,是指声明,从,更新等声明的顺序。语句。 我拥有的碎片是:
DECLARE Var1 INT -- This is the day of week from Field5 in Table2
Var2 DATE -- This is the next calendar date to use in the update
SET Var2 = [calculation for next calendar date]
UPDATE Table1.
SET Field6 = Var2
,Field7 = Var2
,Field8 = Var2
FROM Table1
INNER JOIN Table2 on Table1.Field1 = Table2.Field1 and
Table1.Field2 = Table2.Field2
WHERE Table1.Field3 IS NULL and Table2.Field4 is 'VALUE'
toploting可能会有所帮助:
-- Declare variables
DECLARE @Var1 INT;
DECLARE @Var2 DATE;
-- Set the day of the week from Table2
SELECT @Var1 = t2.Field5
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Field1 = t2.Field1 AND t1.Field2 = t2.Field2
WHERE t1.Field3 IS NULL AND t2.Field4 = 'VALUE';
-- Calculate the next occurring calendar date for the given day of the week
SET @Var2 = DATEADD(DAY, (7 - DATEPART(WEEKDAY, GETDATE()) + @Var1) % 7, GETDATE());
-- Update Table1 with the calculated date
UPDATE t1
SET Field6 = @Var2,
Field7 = @Var2,
Field8 = @Var2
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Field1 = t2.Field1 AND t1.Field2 = t2.Field2
WHERE t1.Field3 IS NULL AND t2.Field4 = 'VALUE';