I have to update the top 2 rows of a column based on the select top 2 statement with some expressions.
update top(2) [Travel].[dbo].[HOTELS]
set [Travel].[dbo].[HOTELS].NAME = (select top(2) SUBSTRING(Name, 1, 5) + 'xxxxx' + SUBSTRING(Name, LEN(Name) - 2, LEN(Name)) AS column1
from [Travel].[dbo].[HOTELS]
)
With the above query I'm getting this error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, ,=, <, <=, >. >= or when the subquery is used as an expression.
I have to use this in both SQL Server and in Oracle.
in sql-server you can use cte + select data first
then update it.
CREATE TABLE HOTELS ([NAME] varchar(14)); INSERT INTO HOTELS ([NAME]) VALUES ('ABCDEFG'), ('HIJKLMOP'); GO
with cte as ( select top 2 NAME, SUBSTRING(Name, 1, 5) + 'xxxxx' + SUBSTRING(Name, LEN(Name) - 2, LEN(Name)) AS column1 from [HOTELS] ) update cte set NAME = column1; GO
2 rows affected
select * from HOTELS;
| NAME | |:------------ | | ABCDExxxxxEFG | | HIJKLxxxxxMOP |
db<>fiddle here
edit for oracle version
CREATE TABLE Table1 ("NAME" varchar2(80));
✓
INSERT ALL INTO Table1 ("NAME") VALUES ('ABCDEFG') INTO Table1 ("NAME") VALUES ('HIJKLMOP') SELECT * FROM dual;
2 rows affected
update Table1 set name = SUBSTR(Name, 1, 5) || 'xxxxx' || SUBSTR(Name, LENGTH(Name) - 2, LENGTH(Name)-1) where rownum <= 2
2 rows affected
select * from Table1
| NAME | |:------------ | | ABCDExxxxxEFG | | HIJKLxxxxxMOP |
db<>fiddle here
In your subquery you have top 2 change the limit to 1
update top 2 [Travel].[dbo].[HOTELS]
set [Travel].[dbo].[HOTELS].NAME = (
select top 1 SUBSTRING(Name, 1, 5) + 'xxxxx' + SUBSTRING(Name, LEN(Name) - 2, LEN(Name)) AS column1
from [Travel].[dbo].[HOTELS]
where [Travel].[dbo].[HOTELS].id = 1415
)
And if you don't want the where condition then don't use it eg:
update top 2 [Travel].[dbo].[HOTELS]
set [Travel].[dbo].[HOTELS].NAME = (
select top 1 SUBSTRING(Name, 1, 5) + 'xxxxx' + SUBSTRING(Name, LEN(Name) - 2, LEN(Name)) AS column1
from [Travel].[dbo].[HOTELS]
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.