postrgresql - 在一个查询中组合多个更新

时间:2018-01-31 13:22:59

标签: sql postgresql

我有以下两个更新查询:

update nodes set path = 'A.S' || 
case 
    when nlevel(path) > nlevel('A.C') then subpath(path, nlevel('A.C'))
    when nlevel(path) = nlevel('A.C') then ''
end
where path <@ 'A.C'



update nodes set id = 'A.S' where id = 'A.C'

如果我可以将这些多个更新组合成一个像:

,那将是完美的
update nodes set path = 'A.S' || 
case 
    when nlevel(path) > nlevel('A.C') then subpath(path, nlevel('A.C'))
    when nlevel(path) = nlevel('A.C') then ''
end
where path <@ 'A.C'

and

update nodes set id = 'A.S' where id = 'A.C'

我一直在寻找这样做的可能性而无法找到,

实际上可能吗?

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:2)

像往常一样:

update nodes 
  set path = 'A.S' || 
    case 
        when path <@ 'A.C' and nlevel(path) > nlevel('A.C') then subpath(path, nlevel('A.C'))
        when path <@ 'A.C' and nlevel(path) = nlevel('A.C') then ''
    else 
      path
    end
  , id = case when id = 'A.C' then 'A.S' else id end 
where path <@ 'A.C' or id = 'A.C'

尝试交易 - 我没有测试过它!