Update statement error: Subquery returned more than 1 value

心不动则不痛 提交于 2019-12-11 01:09:02

问题


I am trying to update all records in a column so that they start with 'CD' e.g. DCE206 would become CDE206.

UPDATE table
SET column = REPLACE(column1, 'DC', 'CD')
WHERE column1 LIKE 'DC%'

I am using the above update statement however the following error appears

'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.'

Is there anything I can change in the statement to make this happen or do I need to look into using a cursor.

I am using SQL Server 2000.


回答1:


You can't possibly be getting this error from that code. The error msut be from some other peice of code.

Do you have anything than runs on update? A trigger?




回答2:


That statement you posted will not generate that error: it has no sub-query.




回答3:


This seems to work fine on my side, 2005. Is this part of a batch of queries, perhaps another update is causing this message?

Edit: Ran this on SQL 2000, no errors.

create table table1 (id int identity, column1 varchar(10))

insert into table1 values ('DCE1')
insert into table1 values ('DCE2')
insert into table1 values ('DCE3')
insert into table1 values ('DCE4')
insert into table1 values ('DCE5')

UPDATE table1
SET column1 = REPLACE(column1, 'DC', 'CD')
WHERE column1 LIKE 'DC%'

select * from table1 
drop table table1



回答4:


You seem to have a typo. It should be:

"SET column1 = "

instead of

"SET column = "

(there is a missing '1')




回答5:


If you're getting the error you indicate from a simple UPDATE, then most likely you have an UPDATE trigger on the table, and the error is coming from the trigger.

However, the code you posted is certainly not the code you are having trouble with, because "table" is a keyword and can't be the name of the table you're updating.



来源:https://stackoverflow.com/questions/1660755/update-statement-error-subquery-returned-more-than-1-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!