在没有一对一关系的情况下从一张表更新另一张表

发布于 2024-09-10 17:16:19 字数 1292 浏览 3 评论 0原文

我正在尝试填充一个日期表,其中包含未来 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)

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

I am trying to populate a date table which contains every date for the next 35 years with information regarding each day.

My ERP system has an accounting years table (GLRULE) which specifies the accounting periods for each specific year since many companies do not work on Calendar Months.

The GLRule table contains one record for each period and looks like this:

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

Anyway, I can update my date table one field at a time with a query similar to this:

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

Is there a better way to update multiple fields at a time? I'm not sure how I can do so since I do not have a join.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

非要怀念 2024-09-17 17:16:19

听起来您尝试更新的表对于每个日期都有一条记录。答案是这样假设的。如果您可以确保您的 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

如果您有兴趣,这里有一个一些测试数据的概念证明

It sounds like the table you're trying to update has one record for every date. The answer assumes that. If you can ensure that your GL datasource has no overlapping dates:

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

If you're interested, here's a proof of concept on some test data.

花之痕靓丽 2024-09-17 17:16:19

您始终可以在更新查询中设置更多字段:

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

you can always set more fields in your update query:

UPDATE TableName Set Field1 = (Select fnnumber From ....), 
Field2 = (some other query or value), etc.
女皇必胜 2024-09-17 17:16:19

也许循环遍历日期表是一个解决方案?

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

Maybe looping through your date table would be a solution?

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文