【发布时间】:2023-01-12 03:22:51
【问题描述】:
我有 PostgreSQL SQL,它应该在名为 source_username 的列中查找反斜杠,如果它找到反斜杠,它应该用相同的值替换 source_username 列的当前值,但反斜杠前没有字符。
例如:
在 source_username 之前:域\用户名
在 source_username 之后:用户名
with os_user as (
select source_username from itpserver.managed_incidents mi;
),
osUserWithoutDomain as (
select (
case when (select * from os_user) ilike '%\\%' and (select position('-' in (select * from os_user))>= 1) and (select length((select * from os_user)) != (select position('-' in (select * from os_user))) + 1)
then (
select substring(
(select * from os_user),(select position('\' in (select * from os_user)) + 1),(select length((select * from os_user)) - 1)
))
else ((select * from os_user))
end
)
)
UPDATE itpserver.managed_incidents SET source_username = replace(source_username, (select * from os_user), (select * from osUserWithoutDomain)),
description = replace(description , (select * from os_user), (select * from osUserWithoutDomain)),
additional_info = replace(additional_info , (select * from os_user), (select * from osUserWithoutDomain)),
typical_behavior = replace(typical_behavior , (select * from os_user), (select * from osUserWithoutDomain)),
raw_description = replace(raw_description , (select * from os_user), (select * from osUserWithoutDomain));
当表中只有一行时,此 SQL 工作正常。
如果我有多行,我需要通过添加 where id = <id> 来指定我想要使用的行
我希望迭代所有相关行(source_username 包含反斜杠的所有行)并在每一行上执行上面的 SQL。
我试着用 LOOP 来做到这一点:
create or replace function fetcher()
returns void as $$
declare
emp record;
begin
for emp in select *
from itpserver.managed_incidents
order by id
limit 10
loop
raise notice '%', emp.id;
<my sql> where id = emp.id
end loop;
end;
$$language plpgsql;
select fetcher();
但是,我收到一个错误,因为我认为它不喜欢“with”语句。
知道我该怎么做吗?
【问题讨论】:
-
当我在表中只有一行时,此 SQL 工作正常....... 所以:这个想法是 case/when 使用一个值,而不是记录集。例如
sql case when (select * from os_user) ilike '%\\%'如果这个select返回多条记录,它已经是一个记录集。 case/when 不知道要取哪条记录,因为不止一条。所以它不再是标量。 -
那将是单个
UPDATE语句而不是循环。
标签: sql postgresql