是否可以在 case 语句中放入 select 语句?

Is it possible to put a select statement in a case statement?

我目前正在尝试在 select 语句中使用 select 语句。到目前为止效果很好。但我希望它 select 根据某些条件有所不同。我的问题是将 SQL 语句子查询放在 CASE 语句的 THEN 部分而不是 WHEN 部分。所以我的例子是here。所以我想在 CASE 语句中放置一个 select 语句,该语句全部包含在子查询中。我也在使用 SQL 服务器 2008。

这是我原来的:

    SELECT DISTINCT
       WORK_ORDER.PART_ID,
       OPERATION.SEQUENCE_NO,
       OPERATION.RESOURCE_ID,
       OPERATION.SETUP_HRS,
       OPERATION.RUN_HRS,
       OPERATION.OPERATION_TYPE,
       OPERATION.RUN AS PCS_HR,
       REPLACE(CONVERT( VARCHAR(150), CONVERT(BINARY(150), OPERATION_BINARY.BITS)), CHAR(0), '') AS Specs_OPR,
       OPERATION.WORKORDER_BASE_ID,
       OPERATION.WORKORDER_LOT_ID,
       OPERATION.WORKORDER_SPLIT_ID,
       OPERATION.WORKORDER_SUB_ID,
       OPERATION.LOAD_SIZE_QTY,
       OPERATION.CALC_START_QTY,
       OPERATION.COMPLETED_QTY,
       (CEILING(OPERATION.CALC_START_QTY / OPERATION.LOAD_SIZE_QTY)) AS NUM_O_LOADS,
       ((OPERATION.CALC_START_QTY - OPERATION.COMPLETED_QTY) / OPERATION.LOAD_SIZE_QTY) AS LOADS_REMAINING,

       -- SubQuery

(
    SELECT DISTINCT
           descriptions = STUFF(
                               (
                                   SELECT ', '+CHAR(13)+PART.DESCRIPTION
                                   FROM dbo.PART
                                        JOIN REQUIREMENT ON PART.ID = REQUIREMENT.PART_ID
                                   WHERE REQUIREMENT.WORKORDER_BASE_ID = '026877'
                                         AND REQUIREMENT.OPERATION_SEQ_NO = '30'
                                   FOR XML PATH(''), TYPE
                               ).value('.[1]', 'nvarchar(max)'), 1, 2, '')
    FROM(REQUIREMENT R
         JOIN PART P ON R.PART_ID = P.ID)
) AS PART_DESCRIPTION

-- End of SubQuery

FROM OPERATION
     INNER JOIN WORK_ORDER ON OPERATION.WORKORDER_TYPE = WORK_ORDER.TYPE
                              AND OPERATION.WORKORDER_BASE_ID = WORK_ORDER.BASE_ID
                              AND OPERATION.WORKORDER_LOT_ID = WORK_ORDER.LOT_ID
                              AND OPERATION.WORKORDER_SPLIT_ID = WORK_ORDER.SPLIT_ID
                              AND OPERATION.WORKORDER_SUB_ID = WORK_ORDER.SUB_ID
     INNER JOIN OPERATION_BINARY ON OPERATION.WORKORDER_TYPE = OPERATION_BINARY.WORKORDER_TYPE
                                    AND OPERATION.WORKORDER_BASE_ID = OPERATION_BINARY.WORKORDER_BASE_ID
                                    AND OPERATION.WORKORDER_LOT_ID = OPERATION_BINARY.WORKORDER_LOT_ID
                                    AND OPERATION.WORKORDER_SPLIT_ID = OPERATION_BINARY.WORKORDER_SPLIT_ID
                                    AND OPERATION.WORKORDER_SUB_ID = OPERATION_BINARY.WORKORDER_SUB_ID
                                    AND OPERATION.SEQUENCE_NO = OPERATION_BINARY.SEQUENCE_NO
     JOIN REQUIREMENT ON REQUIREMENT.WORKORDER_BASE_ID = OPERATION.WORKORDER_BASE_ID
WHERE(REPLACE(CONVERT(VARCHAR(8000), CONVERT(BINARY(8000), OPERATION_BINARY.BITS)), CHAR(0), '') NOT LIKE '%Gupta%')
     AND OPERATION.WORKORDER_BASE_ID = '026877'
     AND WORK_ORDER.BASE_ID = '026877'; 

这就是我现在所拥有的,但出于某种原因,我的案例在我的第 3 行结果中创建了第 4 行:

SELECT DISTINCT
       WORK_ORDER.PART_ID,
       OPERATION.SEQUENCE_NO,
       OPERATION.RESOURCE_ID,
       OPERATION.SETUP_HRS,
       OPERATION.RUN_HRS,
       OPERATION.OPERATION_TYPE,
       OPERATION.RUN AS PCS_HR,
       REPLACE(CONVERT( VARCHAR(150), CONVERT(BINARY(150), OPERATION_BINARY.BITS)), CHAR(0), '') AS Specs_OPR,
       OPERATION.WORKORDER_BASE_ID,
       OPERATION.WORKORDER_LOT_ID,
       OPERATION.WORKORDER_SPLIT_ID,
       OPERATION.WORKORDER_SUB_ID,
       OPERATION.LOAD_SIZE_QTY,
       OPERATION.CALC_START_QTY,
       OPERATION.COMPLETED_QTY,
       (CEILING(OPERATION.CALC_START_QTY / OPERATION.LOAD_SIZE_QTY)) AS NUM_O_LOADS,
       ((OPERATION.CALC_START_QTY - OPERATION.COMPLETED_QTY) / OPERATION.LOAD_SIZE_QTY) AS LOADS_REMAINING,

       -- SubQuery Inside Case

       CASE
           WHEN REQUIREMENT.OPERATION_SEQ_NO = '10'
                AND OPERATION.SEQUENCE_NO = '10'
           THEN
(
    SELECT DISTINCT
           descriptions = STUFF(
                               (
                                   SELECT ', '+CHAR(13)+PART.DESCRIPTION
                                   FROM dbo.PART
                                        JOIN REQUIREMENT ON PART.ID = REQUIREMENT.PART_ID
                                   WHERE REQUIREMENT.WORKORDER_BASE_ID = '026877'
                                         AND REQUIREMENT.OPERATION_SEQ_NO = '10'
                                   FOR XML PATH(''), TYPE
                               ).value('.[1]', 'nvarchar(max)'), 1, 2, '')
    FROM(REQUIREMENT R
         JOIN PART P ON R.PART_ID = P.ID)
)
       END AS DESCRIPTION

-- End of Subquery

FROM OPERATION
     INNER JOIN WORK_ORDER ON OPERATION.WORKORDER_TYPE = WORK_ORDER.TYPE
                              AND OPERATION.WORKORDER_BASE_ID = WORK_ORDER.BASE_ID
                              AND OPERATION.WORKORDER_LOT_ID = WORK_ORDER.LOT_ID
                              AND OPERATION.WORKORDER_SPLIT_ID = WORK_ORDER.SPLIT_ID
                              AND OPERATION.WORKORDER_SUB_ID = WORK_ORDER.SUB_ID
     INNER JOIN OPERATION_BINARY ON OPERATION.WORKORDER_TYPE = OPERATION_BINARY.WORKORDER_TYPE
                                    AND OPERATION.WORKORDER_BASE_ID = OPERATION_BINARY.WORKORDER_BASE_ID
                                    AND OPERATION.WORKORDER_LOT_ID = OPERATION_BINARY.WORKORDER_LOT_ID
                                    AND OPERATION.WORKORDER_SPLIT_ID = OPERATION_BINARY.WORKORDER_SPLIT_ID
                                    AND OPERATION.WORKORDER_SUB_ID = OPERATION_BINARY.WORKORDER_SUB_ID
                                    AND OPERATION.SEQUENCE_NO = OPERATION_BINARY.SEQUENCE_NO
     JOIN REQUIREMENT ON REQUIREMENT.WORKORDER_BASE_ID = OPERATION.WORKORDER_BASE_ID
WHERE(REPLACE(CONVERT(VARCHAR(8000), CONVERT(BINARY(8000), OPERATION_BINARY.BITS)), CHAR(0), '') NOT LIKE '%Gupta%')
     AND OPERATION.WORKORDER_BASE_ID = '026877'
     AND WORK_ORDER.BASE_ID = '026877';

我找到了解决我的具体问题的方法。这只是我的 where 子句导致添加的行。

但是要回答最初的问题,您能否在 CASE 中放置一个子查询 select 语句。是的,你可以做到。

CASE
   WHEN condition
   THEN (select column1, from tbl join tbl2 on tbl1.column = tbl2.column)

   ELSE 
    (whatever)

END