0
I am having difficulties to perform an update with select CTE. I want to update the charging field. Below is the query and just below is the update I tried to perform, but the following error occurs: "The 'historic' derived table is not updatable because a column of the derived table is derived or constant"
-> QUERY
WITH historico AS (
SELECT t.taridinclusao AS tarefa_inclusao
FROM tarefa t
WHERE t.tarid = 167920 and t.taridinclusao IS NOT NULL
UNION ALL
SELECT t.taridinclusao AS tarefa_inclusao
FROM historico h
INNER JOIN tarefa t ON t.tarid = h.tarefa_inclusao
WHERE h.tarefa_inclusao IS NOT NULL and t.taridinclusao IS NOT NULL
)
SELECT top(1)h.*
FROM historico h
ORDER BY h.tarefa_inclusao asc
OPTION(MAXRECURSION 0);
-> UPDATE
WITH historico
AS
(SELECT t.taridinclusao AS tarefa_inclusao
FROM tarefa t
WHERE t.tarid = 167920 and t.taridinclusao IS NOT NULL
UNION ALL
SELECT t.taridinclusao AS tarefa_inclusao
FROM historico h
INNER JOIN tarefa t ON t.tarid = h.tarefa_inclusao
WHERE h.tarefa_inclusao IS NOT NULL and t.taridinclusao IS NOT NULL)
UPDATE historico
SET tarefa_inclusao = tarefa_inclusao
SELECT top(1)h.*
FROM historico h
ORDER BY h.tarefa_inclusao asc
OPTION(MAXRECURSION 0);
Someone could ?
– Renan Bessa
What is the purpose of this
query
? The way you put it you’re givingupdate
in a table that is only the result of aCTE
and exists only in this scope, outside that you are updating a column with itself...– Sorack
Update
has notselect
in its structure– Jefferson Quesado