如何在使用update的嵌套子查询中使用外部查询变量?

6kkfgxo0  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(405)
update fee_schedule fs set fs.branch_id=
(
SELECT  branch_id  FROM   (SELECT  branch_id
                                       ,updated_ts
                                       ,gr_number
                                      ,row_number() OVER (PARTITION BY gr_number ORDER BY updated_ts desc) AS sno
                                FROM   admission_log ) x
                       WHERE  x.gr_number = fs.gr_number and to_char(x.updated_ts,'yyyymm') < fs.schedule_month
                       and rownum=1
                       order by sno
)
where fs.branch_id is null ;

sql错误:ora-00907:缺少右括号00907。00000-“右括号缺失”
我试着用的是同样的东西:

update fee_schedule fs set fs.branch_id=
(
select c.branch_id
      from (select b.branch_id,updated_ts,
                   row_number() over(PARTITION by b.gr_number order by b.updated_ts desc) as Sno
                   from admission_log b
                   where b.gr_number = fs.gr_number and to_char(b.updated_ts,'yyyymm') <  fs.schedule_month ) c
      where c.sno = 1
)
where fs.branch_id is null ;

我发现这个错误:
sql错误:ora-00904:“fs.”“schedule\u month”:标识符无效

x33g5p2x

x33g5p2x1#

你可以用 keep :

update fee_schedule fs
    set fs.branch_id =
            (select max(b.branch_id) keep (dense_rank first order by updated_ts desc)
              from admission_log al
                   where al.gr_number = fs.gr_number and
                         to_char(al.updated_ts, 'yyyymm') < fs.schedule_month 
            )
where fs.branch_id is null ;

此结构相当于“first()”聚合函数,可以方便地绕过oracle对相关子查询的作用域限制。

相关问题