更新查询、嵌套iif集和联接表

ivqmmu1c  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(393)

尝试使用内部联接进行更新查询,并使集合反映当前月数。
需要语法帮助来更新包含月份列的联接表,需要更新月份(date())+1,+2。尝试使用下面嵌套的iif作为“iif(month(date())=”1”,(set table.c=table2.c2……)完成下面的代码。
结果是语法错误
使用用于更新的值更新sql tablelinked excel的示例数据

Update dbo_MasterSalesForecast_Test

INNER JOIN MasterSalesForecastUpdate

ON (dbo_MasterSalesForecast_Test.SubTo=MasterSalesForecastUpdate.Item) AND( dbo_MasterSalesForecast_Test.Planner=MasterSalesForecastUpdate.Planner)

IIF(Month(Date()) ="1",( SET

dbo_MasterSalesForecast_Test.[YY:1] = [MasterSalesForecastUpdate].[YYJan],

dbo_MasterSalesForecast_Test.[YY:2] = [MasterSalesForecastUpdate].[YYFan],

dbo_MasterSalesForecast_Test.[YY:3] = [MasterSalesForecastUpdate].[YYMar])

,

IIF(Month(Date())="2",( SET

dbo_MasterSalesForecast_Test.[YY:2]=[MasterSalesForecastUpdate].[YYFeb],

dbo_MasterSalesForecast_Test.[YY:3]=[MasterSalesForecastUpdate].[YYMar],

dbo_MasterSalesForecast_Test.[YY:4]=[MasterSalesForecastUpdate].[YYApr])

,

IIF(Month(Date())="3",( SET

dbo_MasterSalesForecast_Test.[YY:3]=[MasterSalesForecastUpdate].[YYMar],

dbo_MasterSalesForecast_Test.[YY:4]=[MasterSalesForecastUpdate].[YYApr],

dbo_MasterSalesForecast_Test.[YY:5]=[MasterSalesForecastUpdate].[YYMay])

,

IIF(Month(Date())="4",(SET

dbo_MasterSalesForecast_Test.[YY:4]=[MasterSalesForecastUpdate].[YYApr],

dbo_MasterSalesForecast_Test.[YY:5]=[MasterSalesForecastUpdate].[YYMay],

dbo_MasterSalesForecast_Test.[YY:6]=[MasterSalesForecastUpdate].[YYJun])

,

IIF(Month(Date())="5",(SET

dbo_MasterSalesForecast_Test.[YY:5]=[MasterSalesForecastUpdate].[YYMay],

dbo_MasterSalesForecast_Test.[YY:6]=[MasterSalesForecastUpdate].[YYJun],

dbo_MasterSalesForecast_Test.[YY:7]=[MasterSalesForecastUpdate].[YYJul])

,

IIF(Month(Date())="6",(SET

dbo_MasterSalesForecast_Test.[YY:6]=[MasterSalesForecastUpdate].[YYJun],

dbo_MasterSalesForecast_Test.[YY:7]=[MasterSalesForecastUpdate].[YYJul],

dbo_MasterSalesForecast_Test.[YY:8]=[MasterSalesForecastUpdate].[YYAug])

,

IIF(Month(Date())="7",(SET

dbo_MasterSalesForecast_Test.[YY:7]=[MasterSalesForecastUpdate].[YYJul],

dbo_MasterSalesForecast_Test.[YY:8]=[MasterSalesForecastUpdate].[YYAug],

dbo_MasterSalesForecast_Test.[YY:9]=[MasterSalesForecastUpdate].[YYSep])

,

IIF(Month(Date())="8",(SET

dbo_MasterSalesForecast_Test.[YY:8]=[MasterSalesForecastUpdate].[YYAug],

dbo_MasterSalesForecast_Test.[YY:9]=[MasterSalesForecastUpdate].[YYSep],

dbo_MasterSalesForecast_Test.[YY:10]=[MasterSalesForecastUpdate].[YYOct])

,

IIF(Month(Date())="9",(SET

dbo_MasterSalesForecast_Test.[YY:9]=[MasterSalesForecastUpdate].[YYSep],

dbo_MasterSalesForecast_Test.[YY:10]=[MasterSalesForecastUpdate].[YYOct],

dbo_MasterSalesForecast_Test.[YY:11]=[MasterSalesForecastUpdate].[YYNov])

,

IIF(Month(Date())="10",(SET

dbo_MasterSalesForecast_Test.[YY:10]=[MasterSalesForecastUpdate].[YYOct],

dbo_MasterSalesForecast_Test.[YY:11]=[MasterSalesForecastUpdate].[YYNov],

dbo_MasterSalesForecast_Test.[YY:12]=[MasterSalesForecastUpdate].[YYDec])

,

IIF(Month(Date())="11",(SET

dbo_MasterSalesForecast_Test.[YY:11]=[MasterSalesForecastUpdate].[YYNov],

dbo_MasterSalesForecast_Test.[YY:12]=[MasterSalesForecastUpdate].[YYDec],

dbo_MasterSalesForecast_Test.[YY+1:1]=[MasterSalesForecastUpdate].[YY+1Jan])

,

IIF(Month(Date())="12"(SET

dbo_MasterSalesForecast_Test.[YY:12]=[MasterSalesForecastUpdate].[YYDec],

dbo_MasterSalesForecast_Test.[YY+1:1]=[MasterSalesForecastUpdate].[YY+1Jan],

dbo_MasterSalesForecast_Test.[YY+1:02]=[MasterSalesForecastUpdate].[YY+1Feb])

)))))))))));
6ss1mwsb

6ss1mwsb1#

不能有条件地引用字段。字段在查询对象中必须是静态的,然后可以在iif()中为每个字段确定要更新的值。使用vba构建和执行action sql语句。

CurrentDb.Execute "UPDATE dbo_MasterSalesForecast_Test " & _
     "INNER JOIN MasterSalesForecastUpdate " & _
     "ON (dbo_MasterSalesForecast_Test.SubTo=MasterSalesForecastUpdate.Item) " & _
          "AND (dbo_MasterSalesForecast_Test.Planner=MasterSalesForecastUpdate.Planner) " & _
     "SET [YY:" & Month(Date()) & "] = [YY" & Format(Date(),"mmm") & "]," & _
         "[YY:" & Month(DateAdd("m",1,Date())) & "] = [YY" & Format(DateAdd("m",1,Date()),"mmm") & "]," & _
         "[YY:" & Month(DateAdd("m",2,Date())) & "] = [YY" & Format(DateAdd("m",2,Date()),"mmm") & "]"

相关问题