根据加入SQL Server

时间:2018-04-25 05:14:33

标签: sql sql-server sql-server-2008

我想从表[DL_TRN]中获取数据。如果该表中没有记录,那么我想从另一个表[D_Loan]获取详细信息,[FL_TRN]表和[F_LOAN]的条件相同。我只提到了两个贷款表(需求贷款和节日贷款)但我有在同一个查询中使用多种类型的贷款表。我试过

SELECT 
    m.[CODE], m.[NAME], d.[M_CODE], f.[M_CODE]
FROM 
    [MsumCOOP].[dbo].[MEMBER] m
FULL OUTER JOIN 
    (IF (SELECT COUNT(*) FROM [MsumCOOP].[dbo].[DL_TRN]  
         WHERE [M_CODE] = 6162) > 0
      (SELECT [M_CODE] 
       FROM [MsumCOOP].[dbo].[DL_TRN] 
       WHERE [LOAN_COMP] IS NULL)
    ELSE
       (SELECT [M_CODE] FROM [MsumCOOP].[dbo].[D_Loan] 
        WHERE [LOAN_COMP] IS NULL)) d ON m.[CODE] = d.[M_CODE]
FULL OUTER JOIN 
    (IF (SELECT COUNT(*) FROM [MsumCOOP].[dbo].[FL_TRN]  
         WHERE [M_CODE] = 6162) > 0
        (SELECT [M_CODE] FROM [MsumCOOP].[dbo].[FL_TRN] 
         WHERE [LOAN_COMP] IS NULL)
     ELSE
        (SELECT [M_CODE] FROM [MsumCOOP].[dbo].[F_Loan] 
         WHERE [LOAN_COMP] IS NULL)) f ON m.[CODE] = f.[M_CODE]
WHERE 
    m.code = 6162

但是我收到了这个错误:

  

Msg 156,Level 15,State 1,Line 3
  关键字“IF”附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 7
  ')'附近的语法不正确。

请提出解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用以下

export class ImageUploaderComponent implements OnInit {
  @Input() canvasheight: number;
  @ViewChild('cropper', undefined)
  ...
}

尝试使用变量

SELECT m.[CODE],m.[NAME],d.[OP_AMT]
FROM [MsumCOOP].[dbo].[MEMBER] m
FULL OUTER JOIN
  (
    SELECT [M_CODE] FROM [MsumCOOP].[dbo].[DL_TRN] WHERE [LOAN_COMP] IS NULL AND (SELECT COUNT(*) FROM [MsumCOOP].[dbo].[DL_TRN]  WHERE [M_CODE]=6162)>0
    UNION ALL
    SELECT [M_CODE] FROM [MsumCOOP].[dbo].[D_Loan] WHERE [LOAN_COMP] IS NULL AND (SELECT COUNT(*) FROM [MsumCOOP].[dbo].[DL_TRN]  WHERE [M_CODE]=6162)=0
  ) d
ON m.[CODE]=d.[M_CODE]
FULL OUTER JOIN
  (
    SELECT [M_CODE] FROM [MsumCOOP].[dbo].[FL_TRN] WHERE [LOAN_COMP] IS NULL AND (SELECT COUNT(*) FROM [MsumCOOP].[dbo].[FL_TRN]  WHERE [M_CODE]=6162)>0
    UNION ALL
    SELECT [M_CODE] FROM [MsumCOOP].[dbo].[F_Loan] WHERE [LOAN_COMP] IS NULL AND (SELECT COUNT(*) FROM [MsumCOOP].[dbo].[FL_TRN]  WHERE [M_CODE]=6162)=0
  ) f
ON m.[CODE]=f.[M_CODE]
WHERE m.code=6162