質問

I'm trying to update a single record which is the exact copy of another record. Is there any way to limit or select only 1 record while updating?

Thanks

役に立ちましたか?

解決

You can use FETCH FIRST n ROWS clause .

UPDATE 
( SELECT colA  FROM tableName t WHERE <where condition> FETCH FIRST 1 ROW ONLY
) 
SET t.colA= 'newvalue';

他のヒント

Just curious why exactly you have an exact copy of a record? Do you not have some sort of ID field? Can you show what you've tried?

Really, the best way to avoid this issue must be to have an ID field.

Instead of having copies of multiple records which defeats the purpose of a RDBMS. It would be best to create a referencing table to handle a sorting order.

I have a table with a duplicate rows (possibly more) and just happens I needed something like this. I had issues with your answer as I got error:

"t.myVALUE" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.64.106"

but after some tweaking, I arrived at a slight alternative.

UPDATE 
( SELECT t.myVALUE  FROM mytable t 
 WHERE  ( t.mykey = 'duplicatedkeyvalue' ) 
 FETCH FIRST 1 ROW ONLY
) AS z
SET z.myVALUE = 'newvalue';

just seems to need a second alias 'z' for the subselect result

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top