我们如何在不使用 if else 块而不是 within 单个查询的情况下实现逻辑?

How can we implement logic without using if else block instead of the within single query?

SELECT column_key,
        Column_name
FROM tableA

UNION 
SELECT column_key,
        Column_name
FROM tableB
WHERE Database_Name = 'PROD'
  AND Table_Name = 'Document_Setup'
  AND Column_Name IN
      (
        'App_Advice',
        'Expect_Response_Time',
        'Matching_Interchanges',
        'Transmit')

现在我想在第二个 table 添加新的列检查条件,当 Doc_Standard_Name ='830' 可用时,然后添加列名称“Multiple_Transaction_Set”,否则它会像什么一样工作目前它的工作 但我不想用两个大的 if 语句块编写逻辑。可以用case Statement或者其他方式实现吗?

 SELECT
      *
    FROM Document_Standard
    WHERE Document_key = @Document_key
      AND Doc_Standard_Name IN ('830')


SELECT column_key,
        Column_name
FROM tableA

UNION 
SELECT column_key,
        Column_name
FROM tableB
WHERE Database_Name = 'PROD'
  AND Table_Name = 'Document_Setup'
  AND Column_Name IN
      (
        'App_Advice',
        'Expect_Response_Time',
        'Matching_Interchanges',
        'Transmit',
        'Multiple_Transaction_Set')

我是这样写的,但不想写在 if else 块中,而是我想在一个块中处理:

IF EXISTS

  (
     SELECT
      *
    FROM Document_Standard
    WHERE Document_key = @Document_key
      AND Doc_Standard_Name IN ('830')
  )
 BEGIN
    SELECT column_key,
            Column_name
    FROM tableA
    
    UNION 
    SELECT column_key,
            Column_name
    FROM tableB
    WHERE Database_Name = 'PROD'
  AND Table_Name = 'Document_Setup'
  AND Column_Name IN
        (
         'App_Advice',
         'Expect_Response_Time',
         'Matching_Interchanges',
         'Transmit')
 END
 ELSE
 BEGIN
    SELECT column_key,
        Column_name
    FROM tableA
    
    UNION 
    SELECT column_key,
            Column_name
    FROM tableB
    WHERE Database_Name = 'PROD'
  AND Table_Name = 'Document_Setup'
  AND Column_Name IN
        (
         'App_Advice',
         'Expect_Response_Time',
         'Matching_Interchanges',
         'Transmit',
         'Multiple_Transaction_Set')
            
 END;

随便用一些,OR逻辑:

SELECT column_key,
       Column_name
FROM tableA
UNION --ALL --Should this is UNION? UNION is a significantly more expensive; it should only be used if you need DISTINCT rows
SELECT column_key,
       Column_name
FROM tableA
WHERE Column_Name IN ('App_Advice', 'Expect_Response_Time', 'Matching_Interchanges', 'Transmit')
   OR (EXISTS (SELECT 1
               FROM Document_Standard
               WHERE Document_key = @Document_key
                 AND Doc_Standard_Name IN ('830'))
   AND Column_Name = 'Multiple_Transaction_Set');