CTE后更新:多部分标识符" MyCTE.SomeField"无法受约束

时间:2017-03-31 17:33:03

标签: sql sql-server-2012

我的sql-server中有以下SQL查询:

"isml" : [
  "app_storefront_core/cartridge/templates/default/util/modules.isml"
]
  

多部分标识符" MyCTE.SomeField"无法受约束。

你能帮我删除这个错误吗?
顺便说一下,我有WITH SampleCTE1 AS ( ... ), MyCTE(SomeField) AS ( SELECT SomeField FROM ....) UPDATE SomeTable WITH (serializable) set SomeColumnInSomeTable = MyCTE.SomeField where SomeFieldInSomeTable = 112 if @@rowcount = 0 begin insert into SomeTable..... end 。可以使用sql-server-2012吗?在某些地方,我确实读过因为错误我不应该使用它。你觉得怎么样?

2 个答案:

答案 0 :(得分:0)

您只能在最后一个cte之后的语句中引用您的公用表表达式,因此您必须重复它才能再次使用它来表示以下insert(或使用merge) 。

您还未在MyCte声明中加入或访问update

WITH SampleCTE1 AS ( ... ), 

MyCTE(SomeField) AS ( SELECT SomeField FROM ....) 

UPDATE SomeTable WITH (serializable)
   set SomeColumnInSomeTable = MyCTE.SomeField  
   from SomeTable 
     inner join MyCTE
       on SomeTable.SomeId = MyCte.SomeId
   where SomeFieldInSomeTable = 112;

   if @@rowcount = 0
   begin;

    ;WITH SampleCTE1 AS ( ... ), 

    MyCTE(SomeField) AS ( SELECT SomeField FROM ....) 

      insert into SomeTable.....
   end;

答案 1 :(得分:0)

MyCTE未引用

UPDATE。它应该是

...
UPDATE SomeTable WITH (serializable)
   set SomeColumnInSomeTable = MyCTE.SomeField 
FROM SomeTable
{LEFT/ RIGHT / INNER}JOIN  MyCTE ON ...