简体   繁体   中英

SQL Server Using Case With Two Matching Tables

I have two tables:

  1. Accounts table
  2. Matching_Filter table

Matching filter table contains columns must be matched with same columns in the Accounts table if MATCHING COLUMNS IS SET, where -1 not set value and other value is set value.

Using CASE with WHERE clause was not successful with my code

This is my attempt:

SELECT A.*
FROM dbo.App_Account_Match_Filter F
INNER JOIN dbo.App_Accounts_Data A ON F.Apm_File_Type_LK <> A.Apa_File_Type_LK
WHERE F.Apm_ID = 1
      CASE WHEN F.Apm_Nationality_LK <> -1 THEN  A.Apm_Nationality_LK = F.Apa_Nationality_LK END

CASE is an expression , it returns a scalar value not a boolean value. You need to use proper boolean logic. Seeing as you don't have a ELSE in your CASE expression it seems you just need this:

SELECT A.*
FROM dbo.App_Account_Match_Filter F
     INNER JOIN dbo.App_Accounts_Data A ON F.Apm_File_Type_LK <> A.Apa_File_Type_LK
WHERE F.Apm_ID = 1
  AND F.Apm_Nationality_LK <> -1
  AND A.Apm_Nationality_LK = F.Apa_Nationality_LK;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM