sql:更新日期条件

yhxst69z  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(333)

我想根据两个不同表中的日期更新我的列。下面是我的问题

UPDATE T_TRN_DEAL_DETAILS SET MATURITY_DT  = 
 (CASE
  WHEN (Select top 1 INS.INVOICE_DUE_DT as invoice_date  from T_TRN_INVOICE_DETAILS IND
 INNER jOIN T_TRN_INVOICE_SUMMARY INS on IND.INVOICE_ID=INS.INVOICE_ID
 where IND.DEAL_ID ='1234'
 order by INS.INVOICE_DUE_DT desc ) > (Select DEAL_ID,MATURITY_DT as invoice_date from T_TRN_DEAL_DETAILS WHERE DEAL_ID ='1234')  
 THEN (Select top 1 INS.INVOICE_DUE_DT as invoice_date  from T_TRN_INVOICE_DETAILS IND
 INNER jOIN T_TRN_INVOICE_SUMMARY INS on IND.INVOICE_ID=INS.INVOICE_ID
 where IND.DEAL_ID ='1234'
 order by INS.INVOICE_DUE_DT desc) 
 ELSE (Select DEAL_ID,MATURITY_DT as invoice_date from T_TRN_DEAL_DETAILS WHERE DEAL_ID ='DL18111213586')
 END )  
 WHERE DEAL_ID ='1234'

但我的错误率越来越低
当子查询没有引入exists时,只能在选择列表中指定一个表达式
虽然我只是比较两次约会。

nwlls2ji

nwlls2ji1#

错误很明显:
子查询必须只返回一列,但在else brench中,您编写了以下代码:

(Select DEAL_ID,MATURITY_DT as invoice_date 
from T_TRN_DEAL_DETAILS WHERE DEAL_ID ='DL18111213586')

所以您尝试返回两列。您必须删除 DEAL_ID 列,因为你的 MATURITY_DT 要用于更新主表的字段。
当您尝试比较子查询(>)时在第一个分支中所做的相同错误,其中第二个子查询返回两列而不是一列。

(Select DEAL_ID,MATURITY_DT as invoice_date
from T_TRN_DEAL_DETAILS WHERE DEAL_ID ='1234')

相关问题