Trying to update a table with a unique iD column as there is no unique key column in the same.
When using the below CTE, I am getting an error of relation does not exist for the CTE name in the update statement. but the same is working fine when select statement is executed.
Query used:
With updateid As
(
SELECT
ID,
ROW_NUMBER() OVER (ORDER BY Model DESC) AS RN
FROM aud
)UPDATE updateid SET ID='AUD'||repeat('0',5-length(cast(RN as varchar)))||cast(RN as varchar)
Error encountered:
ERROR: relation "updateid" does not exist LINE 7: )UPDATE updateid SET ID='AUD'+replicate('0',5-len(cast(RN as... ^ SQL state: 42P01 Character: 95
The select statement that worked well:
With updateid As
(
SELECT
ID,
ROW_NUMBER() OVER (ORDER BY Model DESC) AS RN
FROM aud
)Select * from updateid
||- the+is for adding numbers. There also is noreplicate()function in Postgres. And you can't update the result of a CTE in Postgres