I'm having a hard time performing an update with select CTE. I want to refresh the taridinclusao field. Below is the query and just below is the update that I tried to perform, however, the following error occurs: "The 'historical' derived table is not updateable 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);