SQL Server CTE在多种条件下选择

时间:2018-11-20 00:14:32

标签: sql-server

只是想知道正确的方法,在以下情况下我不能使用“ if”,并且我还需要在其上添加更多条件。

我可以通过添加联合来做到这一点,但是效率不高,我不想多次检查是否存在然后选择。

任何最佳方式可以做到吗?谢谢

我要根据匹配的内容进行选择。

 declare @studentID varchar(50) = '1432166';
 declare @firstname varchar(50) = 'Hello';
 declare @emailAddress varchar(100) = 'abc@hello.com';
 declare @mobileNumber varchar(10) = '2312321'

 with byStudentID as 
 (
      select 
          U3l_referenceID,
          preferredfirstname, 
          studentID,
          emailAddress,
          mobileNumber
      from 
          [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
      where 
          studentID = @studentID
),
byfNameEmailAdd as 
(
    select 
        U3l_referenceID,
        preferredfirstname, 
        studentID,
        emailAddress,
        mobileNumber
    from 
        [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
    where 
        preferredfirstname = @firstname 
        and emailAddress = @emailAddress 
),
byMobileNumber as  
(
    select 
        U3l_referenceID,
        preferredfirstname, 
        studentID,
        emailAddress,
        mobileNumber
    from 
        [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] L with(nolock)
    where 
        mobileNumber = @mobileNumber 
        and emailAddress = @emailAddress 
)
IF Exists (select * from byStudentID)
    select * from byStudentID
End
else IF Exists (Select * from byfNameEmailAdd)
    select * from byfNameEmailAdd
End
else if Exists (Select * from byMobileNumber)
    select  * from byMobileNumber
End

2 个答案:

答案 0 :(得分:0)

您可以使用UNION ALL提取通过任何方法找到的任何结果。在这里,我添加了一个新的[SearchType]字段,以便您可以知道结果来自何处:

 DECLARE @studentID VARCHAR(50) = '1432166';
 DECLARE @firstname VARCHAR(50) = 'Hello';
 DECLARE @emailAddress VARCHAR(100) = 'abc@hello.com';
 DECLARE @mobileNumber VARCHAR(10) = '2312321'

  SELECT 
  [SearchType] = 'StudentID',
  [U3l_referenceID],
  [preferredfirstname], 
  [studentID],
  [emailAddress],
  [mobileNumber]
    FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
    WHERE [studentID] = @studentID
UNION ALL
  SELECT 
  [SearchType] = 'FN.Email',
  [U3l_referenceID],
  [preferredfirstname], 
  [studentID],
  [emailAddress],
  [mobileNumber]
    FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
    WHERE [preferredfirstname] = @firstname 
        AND [emailAddress] = @emailAddress
UNION ALL
  SELECT 
  [SearchType] = 'MN.Email',
  [U3l_referenceID],
  [preferredfirstname], 
  [studentID],
  [emailAddress],
  [mobileNumber]
    FROM [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH(NOLOCK)
    WHERE [mobileNumber] = @mobileNumber 
        AND [emailAddress] = @emailAddress

如果您只需要一个答案,只需将整个内容包装在CTE中,然后从中选择TOP 1。

答案 1 :(得分:0)

我刚刚注意到它们全部来自同一张表,因此您可以对嵌套OR语句使用单个查询:

SELECT  [SearchType]    = 'StudentID'
    , [U3l_referenceID]
    , [preferredfirstname]
    , [studentID]
    , [emailAddress]
    , [mobileNumber]
FROM    [u3_data].[data].[ListData_3bf154c40aa84565b9bb08d58ffffff6] [L] WITH (NOLOCK)
WHERE ([studentID] = @studentID)
    OR
    (
        [preferredfirstname] = @firstname
        AND [emailAddress] = @emailAddress
    )
    OR
    (
        [mobileNumber] = @mobileNumber
        AND [emailAddress] = @emailAddress
    ) ;