2010-07-15 75 views
4

我想填充一个日期表,其中包含关于每一天的信息的下一个35年的每个日期。从另一个表更新一个表而不存在一对一的关系

我的ERP系统有一个会计年数表(GLRULE),它指定了每个特定年份的会计期,因为许多公司在“日历月”上不工作。

的GLRule表包含一个记录每个时期,看起来像这样:

fcbasis fcname fcstatus fdend    fdstart   flisadjust flisaudit fnnumber freval identity_column 
A  FY 2000 C   1/28/2000 0:00:00 1/1/2000 0:00:00 FALSE  FALSE  1   FALSE 37 
A  FY 2000 C   2/25/2000 0:00:00 1/29/2000 0:00:00 FALSE  FALSE  2   FALSE 38 
A  FY 2000 C   3/31/2000 0:00:00 2/26/2000 0:00:00 FALSE  FALSE  3   FALSE 39 
A  FY 2000 C   4/28/2000 0:00:00 4/1/2000 0:00:00 FALSE  FALSE  4   FALSE 40 
A  FY 2000 C   5/26/2000 0:00:00 4/29/2000 0:00:00 FALSE  FALSE  5   FALSE 41 
A  FY 2000 C   6/30/2000 0:00:00 5/27/2000 0:00:00 FALSE  FALSE  6   FALSE 42 

无论如何,我可以用一个类似的查询时间更新我的约会表中的一个字段:

UPDATE redfridaydates.dbo.testdates 
    SET [FISCAL_PERIOD] = 
       (SELECT fnnumber 
        FROM m2mdata01..glrule GLR 
        where DATE >= GLR.FDSTART and DATE <= GLR.FDEND) 

是否有更好的方法来一次更新多个字段?我不知道我怎么能这样做,因为我没有加入。

+1

请勿在格式化代码时使用选项卡。 – 2010-07-15 21:01:54

+0

@Chadwick:Thx纠正格式! – 2010-07-15 21:40:14

+0

对不起,我没有使用制表符,我从Excel电子表格中复制并粘贴了它。 – DavidStein 2010-07-15 21:52:26

回答

4

这听起来像你想更新表对每一个最新记录。答案假设。如果你能保证你的数据源GL有没有重叠的日期:

UPDATE T 
SET T.[FISCAL_PERIOD] = GLR.fnnumber, 
    T.Foo = GLR.Bar, 
    T.Bat = GLR.Baz --etc. 

FROM redfridaydates.dbo.testdates AS T 
INNER JOIN m2mdata01..glrule AS GLR 
ON T.[Date] BETWEEN GLR.FDSTART AND GLR.FDEND 

如果你有兴趣,这里有一个proof of concept on some test data

+0

谢谢,我会试试看。我无法检查概念证明,因为我的公司阻止了该网站。 – DavidStein 2010-07-16 18:32:36

+1

@大卫 - 也许这个网站没有被封锁 - http://www.copypastecode.com/32817/ – 2010-07-16 19:07:37

1

您可以随时在您的更新查询中设置更多的领域:

UPDATE TableName Set Field1 = (Select fnnumber From ....), 
Field2 = (some other query or value), etc. 
+0

是的,我明白这一点。我希望以此为基础的一套基于处理方式。 – DavidStein 2010-07-15 21:51:50

1

也许循环你的日期表是一个解决方案?

declare @myDate SmallDateTime 
set @myDate = (Select MIN(DATE) FROM dbo.testdates) 

declare @myFiscal int 

WHILE @myDate is not null 
BEGIN 

--determine the fiscal period 
--output a zero for missing fiscal periods 
SET @myFiscal = isnull(
    (
    SELECT fnnumber 
    FROM m2mdata01..glrule GLR 
    WHERE @myDate >= GLR.FDSTART AND @myDate <= GLR.FDEND 
    ),0) 

--update the date table 
UPDATE redfridaydates.dbo.testdates 
SET fiscal_period = @fiscal 
WHERE date = @myDate 

--get the next date 
--will be null once the loop reaches the end of the table 
SET @myDate = (Select MIN(DATE) FROM dbo.testdates WHERE DATE > @myDAte) 

END