更新 cte 'result' 失败,因为它包含派生字段或常量字段
Update a cte 'result' failed because it contains a derived or constant field
我有一个像下面这样的 cte;
;with cte as (field1, field2, field3)
我需要 select 来自此 CTE 的记录,但在我需要对该 CTE 中的其中一个列进行一些更新之前,该更新基于标量函数的输出(它将 return 1 或 0 作为输出(位)),我在下面的 case 语句中使用它;
;with cte as (field1, field2, field3)
update cte
set field1 = (select
case when dbo.scalarFunction (@parm1,@parm2) = 0
then 'New Value'
else cte.field1
end)
from cte
where field2 = 'some filter' and field3 = 'some filter'
select * from cte
执行此操作时出现以下错误;
Update or insert of view or function 'result' failed because it contains a derived or constant field.
任何人都可以请教如何更新我的 cte,然后在 cte 中查看更新的记录
也许您可以在 select 中执行此更改而不是更新命令 -
;with cte as (field1, field2, field3)
select
field1 = (case when field2 = 'some filter' and field3 = 'some filter' then
(case when dbo.scalarFunction (@parm1,@parm2) = 0
then 'New Value'
else cte.field1
end)
else field1 end)
,field2
,field3
from cte
我有一个像下面这样的 cte;
;with cte as (field1, field2, field3)
我需要 select 来自此 CTE 的记录,但在我需要对该 CTE 中的其中一个列进行一些更新之前,该更新基于标量函数的输出(它将 return 1 或 0 作为输出(位)),我在下面的 case 语句中使用它;
;with cte as (field1, field2, field3)
update cte
set field1 = (select
case when dbo.scalarFunction (@parm1,@parm2) = 0
then 'New Value'
else cte.field1
end)
from cte
where field2 = 'some filter' and field3 = 'some filter'
select * from cte
执行此操作时出现以下错误;
Update or insert of view or function 'result' failed because it contains a derived or constant field.
任何人都可以请教如何更新我的 cte,然后在 cte 中查看更新的记录
也许您可以在 select 中执行此更改而不是更新命令 -
;with cte as (field1, field2, field3)
select
field1 = (case when field2 = 'some filter' and field3 = 'some filter' then
(case when dbo.scalarFunction (@parm1,@parm2) = 0
then 'New Value'
else cte.field1
end)
else field1 end)
,field2
,field3
from cte