SQL 带有 WITH 子句的值太多错误
SQL with WITH clause too many values error
--此查询返回 "too many values" 错误。我不确定为什么?
update myTable
set var1= var2
WHERE value1 IN (
WITH X AS
(
select value1, value2, var1,var2, ROW_NUMBER()
OVER
(PARTITION BY value1 ORDER BY value1 desc) as rn
from
mytable WHERE var1 is null AND rownum>0 and rownum<=10 order by value1 asc
)
SELECT
value1, value2, var2,var1
FROM X WHERE rn=1 and var1 is null and rownum>0 and rownum<=10);
错误too many values
是因为查询value1 IN ()
在内部查询中选择了多个列。为避免这种情况,内部查询中只需 select
value1。
update myTable
set var1 = var2
WHERE value1 IN
(select value1 from
(select value1, value2, var1,var2,
ROW_NUMBER() OVER (PARTITION BY value1 ORDER BY value1 desc) as rn
from mytable
WHERE var1 is null
) t
where rn <= 10
)
我认为使用 exists
:
可以更好地满足您的意图
update myTable
set var1 = var2
where exists (select 1
from (select value1, value2, var1, var2,
ROW_NUMBER() OVER (PARTITION BY value1 ORDER BY value1 desc) as rn
from mytable
where var1 is null
) x
where mytable.value1 in (x.value1, x.value2, x.var1, x.var2) and
rn <= 10
);
--此查询返回 "too many values" 错误。我不确定为什么?
update myTable
set var1= var2
WHERE value1 IN (
WITH X AS
(
select value1, value2, var1,var2, ROW_NUMBER()
OVER
(PARTITION BY value1 ORDER BY value1 desc) as rn
from
mytable WHERE var1 is null AND rownum>0 and rownum<=10 order by value1 asc
)
SELECT
value1, value2, var2,var1
FROM X WHERE rn=1 and var1 is null and rownum>0 and rownum<=10);
错误too many values
是因为查询value1 IN ()
在内部查询中选择了多个列。为避免这种情况,内部查询中只需 select
value1。
update myTable
set var1 = var2
WHERE value1 IN
(select value1 from
(select value1, value2, var1,var2,
ROW_NUMBER() OVER (PARTITION BY value1 ORDER BY value1 desc) as rn
from mytable
WHERE var1 is null
) t
where rn <= 10
)
我认为使用 exists
:
update myTable
set var1 = var2
where exists (select 1
from (select value1, value2, var1, var2,
ROW_NUMBER() OVER (PARTITION BY value1 ORDER BY value1 desc) as rn
from mytable
where var1 is null
) x
where mytable.value1 in (x.value1, x.value2, x.var1, x.var2) and
rn <= 10
);