如何在T-Sql中自定义结果

时间:2018-10-08 06:57:22

标签: sql tsql

           (SELECT DISTINCT RTRIM(CB_Account_Code)
            FROM Correspondent_Bank
                 WHERE Correspondent_Bank.CB_Cnt_Code = Contributions.Con_Cnt_Code and
                       Correspondent_Bank.CB_Prv_Code = Contributions.Con_Prv_Code and
                       Correspondent_Bank.CB_Dst_Code = Contributions.Con_Dst_Code and
                       Correspondent_Bank.CB_Cor_Code = Contributions.Con_Cor_Code) as BankAccoutCode

下面给出的我的部分SQL链接。如果找不到符合特定条件的记录,我想知道如何将“找不到记录”移至BankAccountCode。

1 个答案:

答案 0 :(得分:1)

使用coalesce函数返回第一个非空值

select COALESCE( (SELECT DISTINCT RTRIM(CB_Account_Code)
                FROM Correspondent_Bank
                     WHERE Correspondent_Bank.CB_Cnt_Code = Contributions.Con_Cnt_Code and
                           Correspondent_Bank.CB_Prv_Code = Contributions.Con_Prv_Code and
                           Correspondent_Bank.CB_Dst_Code = Contributions.Con_Dst_Code and
                           Correspondent_Bank.CB_Cor_Code = Contributions.Con_Cor_Code) ,'NO RECORDS FOUND') as BankAccoutCode