Sql server 子查询返回的值超过1个无法更正

Sql server 子查询返回的值超过1个无法更正,sql-server,sql-server-2008,Sql Server,Sql Server 2008,尝试将备注变量的填充替换为 Create PROCEDURE [dbo].[USP_ViewRejectedReport24102014] ( @Pvch_AdvertisementNo VARCHAR(200) ,@Pvch_Post_Code VARCHAR(100) ,@Pint_DistrictId INT ,@PintType INT ) AS DECLARE @Remarks VARCHAR(max) SET @Remarks = (

尝试将备注变量的填充替换为

Create PROCEDURE [dbo].[USP_ViewRejectedReport24102014] (
    @Pvch_AdvertisementNo VARCHAR(200)
    ,@Pvch_Post_Code VARCHAR(100)
    ,@Pint_DistrictId INT
    ,@PintType INT
    )
AS
DECLARE @Remarks VARCHAR(max)
SET @Remarks = (
        SELECT distinct C.vch_Remarks
        FROM T_CAF_Receive C,T_ApplicantDetails_Temp A
        WHERE  C.vch_uniquerefno =A.vch_uniquerefno AND ISNULL(c.bit_DeletedFlag, 0) = 0
        )
BEGIN
    IF (@PintType = 1)
    BEGIN
        IF (@Pint_DistrictId = 0)
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,(
                    CASE @Remarks
                        WHEN NULL
                            THEN (
                                    SELECT vch_Remarks
                                    FROM T_CAF_Receive C
                                    WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                        AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                    )
                        ELSE (
                                SELECT STUFF((
                                            SELECT vch_remarks
                                            FROM M_remark_detail
                                            WHERE int_remark_id IN (
                                                    SELECT val
                                                    FROM UDF_Split((
                                                                SELECT vch_remarks
                                                                FROM T_caf_receive C
                                                                WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                                ), ',')
                                                    )
                                            FOR XML PATH('')
                                            ), 1, 1, '')
                                )
                        END
                    ) Remarks
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
            ORDER BY A.vch_UniqueRefNo
        END
        ELSE
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,(
                    CASE @Remarks
                        WHEN NULL
                            THEN (
                                    SELECT vch_Remarks
                                    FROM T_CAF_Receive C
                                    WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                        AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                    )
                        ELSE (
                                SELECT STUFF((
                                            SELECT vch_remarks
                                            FROM M_remark_detail
                                            WHERE int_remark_id IN (
                                                    SELECT val
                                                    FROM UDF_Split((
                                                                SELECT vch_remarks
                                                                FROM T_caf_receive C
                                                                WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                                ), ',')
                                                    )
                                            FOR XML PATH('')
                                            ), 1, 1, '')
                                )
                        END
                    ) Remarks
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
                AND A.int_examDistrictId = @Pint_DistrictId
            ORDER BY A.vch_UniqueRefNo
        END
    END
    ELSE IF (@PintType = 2)
    BEGIN
        IF (@Pint_DistrictId = 0)
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,CASE @Remarks
                    WHEN NULL
                        THEN (
                                SELECT vch_Remarks
                                FROM T_CAF_Receive C
                                WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                    AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                )
                    ELSE (
                            SELECT STUFF((
                                        SELECT vch_remarks
                                        FROM M_remark_detail
                                        WHERE int_remark_id IN (
                                                SELECT val
                                                FROM UDF_Split((
                                                            SELECT vch_remarks
                                                            FROM T_caf_receive C
                                                            WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                            ), ',')
                                                )
                                        FOR XML PATH('')
                                        ), 1, 1, '')
                            )
                    END
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
            ORDER BY A.vch_UniqueRefNo
        END
        ELSE
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,(
                    CASE @Remarks
                        WHEN NULL
                            THEN (
                                    SELECT vch_Remarks
                                    FROM T_CAF_Receive C
                                    WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                        AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                    )
                        ELSE (
                                SELECT STUFF((
                                            SELECT vch_remarks
                                            FROM M_remark_detail
                                            WHERE int_remark_id IN (
                                                    SELECT val
                                                    FROM UDF_Split((
                                                                SELECT vch_remarks
                                                                FROM T_caf_receive C
                                                                WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                                ), ',')
                                                    )
                                            FOR XML PATH('')
                                            ), 1, 1, '')
                                )
                        END
                    ) Remarks
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
                AND A.int_examDistrictId = @Pint_DistrictId
            ORDER BY A.vch_UniqueRefNo
        END
    END
    ELSE IF (@PintType = 3)
    BEGIN
        IF (@Pint_DistrictId = 0)
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,CASE @Remarks
                    WHEN NULL
                        THEN (
                                SELECT vch_Remarks
                                FROM T_CAF_Receive C
                                WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                    AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                )
                    ELSE (
                            SELECT STUFF((
                                        SELECT vch_remarks
                                        FROM M_remark_detail
                                        WHERE int_remark_id IN (
                                                SELECT val
                                                FROM UDF_Split((
                                                            SELECT vch_remarks
                                                            FROM T_caf_receive C
                                                            WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                            ), ',')
                                                )
                                        FOR XML PATH('')
                                        ), 1, 1, '')
                            )
                    END
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
            ORDER BY A.vch_UniqueRefNo
        END
        ELSE
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,(
                    CASE @Remarks
                        WHEN NULL
                            THEN (
                                    SELECT vch_Remarks
                                    FROM T_CAF_Receive C
                                    WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                        AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                    )
                        ELSE (
                                SELECT STUFF((
                                            SELECT vch_remarks
                                            FROM M_remark_detail
                                            WHERE int_remark_id IN (
                                                    SELECT val
                                                    FROM UDF_Split((
                                                                SELECT vch_remarks
                                                                FROM T_caf_receive C
                                                                WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                                ), ',')
                                                    )
                                            FOR XML PATH('')
                                            ), 1, 1, '')
                                )
                        END
                    ) Remarks
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
                AND A.int_examDistrictId = @Pint_DistrictId
            ORDER BY A.vch_UniqueRefNo
        END
    END
    ELSE IF (@PintType = 4)
    BEGIN
        IF (@Pint_DistrictId = 0)
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,CASE @Remarks
                    WHEN NULL
                        THEN (
                                SELECT vch_Remarks
                                FROM T_CAF_Receive C
                                WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                    AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                )
                    ELSE (
                            SELECT STUFF((
                                        SELECT vch_remarks
                                        FROM M_remark_detail
                                        WHERE int_remark_id IN (
                                                SELECT val
                                                FROM UDF_Split((
                                                            SELECT vch_remarks
                                                            FROM T_caf_receive C
                                                            WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                            ), ',')
                                                )
                                        FOR XML PATH('')
                                        ), 1, 1, '')
                            )
                    END
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
            ORDER BY A.vch_UniqueRefNo
        END
        ELSE
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,(
                    CASE @Remarks
                        WHEN NULL
                            THEN (
                                    SELECT vch_Remarks
                                    FROM T_CAF_Receive C
                                    WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                        AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                    )
                        ELSE (
                                SELECT STUFF((
                                            SELECT vch_remarks
                                            FROM M_remark_detail
                                            WHERE int_remark_id IN (
                                                    SELECT val
                                                    FROM UDF_Split((
                                                                SELECT vch_remarks
                                                                FROM T_caf_receive C
                                                                WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                                ), ',')
                                                    )
                                            FOR XML PATH('')
                                            ), 1, 1, '')
                                )
                        END
                    ) Remarks
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
                AND A.int_examDistrictId = @Pint_DistrictId
            ORDER BY A.vch_UniqueRefNo
        END
    END
    ELSE
    BEGIN
        IF (@Pint_DistrictId = 0)
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,CASE @Remarks
                    WHEN NULL
                        THEN (
                                SELECT vch_Remarks
                                FROM T_CAF_Receive C
                                WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                    AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                )
                    ELSE (
                            SELECT STUFF((
                                        SELECT vch_remarks
                                        FROM M_remark_detail
                                        WHERE int_remark_id IN (
                                                SELECT val
                                                FROM UDF_Split((
                                                            SELECT vch_remarks
                                                            FROM T_caf_receive C
                                                            WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                            ), ',')
                                                )
                                        FOR XML PATH('')
                                        ), 1, 1, '')
                            )
                    END
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
            ORDER BY A.vch_UniqueRefNo
        END
        ELSE
        BEGIN
            SELECT A.int_ApplicantId
                ,A.vch_UniqueRefNo
                ,A.vch_ApplicantName
                ,CASE int_Gender
                    WHEN 1
                        THEN 'Male'
                    WHEN 2
                        THEN 'Female'
                    END AS Gender
                ,vch_CorHouseNo
                ,(
                    SELECT vch_DistrictName
                    FROM M_District
                    WHERE int_DistrictID = A.int_CorDistID
                        AND int_StateID = A.int_CorStateID
                    ) District
                ,vch_CorPinCode
                ,dtm_DOB
                ,(
                    CASE int_CategoryId
                        WHEN 1
                            THEN 'UR'
                        WHEN 2
                            THEN 'SC'
                        WHEN 3
                            THEN 'ST'
                        WHEN 4
                            THEN 'SEBC'
                        END
                    ) Category
                ,(
                    CASE isnull(bit_PHOH, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) PWD
                ,(
                    CASE isnull(bit_SportsPerson, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) SportsPerson
                ,(
                    CASE isnull(bit_ESM, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) ESM
                ,(
                    CASE isnull(bit_Odia, 0)
                        WHEN 1
                            THEN 'Yes'
                        ELSE 'No'
                        END
                    ) Odia
                ,(
                    SELECT vch_DDNO
                    FROM T_Finance_Details F
                    WHERE isnull(F.bit_DeletedFlag, 0) = 0
                        AND F.vch_uniquerefno = A.vch_uniquerefno
                    ) vch_DDNO
                ,A.vch_CorMobileNo
                ,(
                    CASE @Remarks
                        WHEN NULL
                            THEN (
                                    SELECT vch_Remarks
                                    FROM T_CAF_Receive C
                                    WHERE C.vch_UniqueRefNo = A.vch_UniqueRefNo
                                        AND ISNULL(c.bit_DeletedFlag, 0) = 0
                                    )
                        ELSE (
                                SELECT STUFF((
                                            SELECT vch_remarks
                                            FROM M_remark_detail
                                            WHERE int_remark_id IN (
                                                    SELECT val
                                                    FROM UDF_Split((
                                                                SELECT vch_remarks
                                                                FROM T_caf_receive C
                                                                WHERE C.vch_uniquerefno = A.vch_uniquerefno
                                                                ), ',')
                                                    )
                                            FOR XML PATH('')
                                            ), 1, 1, '')
                                )
                        END
                    ) Remarks
                ,(
                    CASE isnull(A.bit_ReceiveStatus, 0)
                        WHEN 1
                            THEN '1'
                        ELSE '0'
                        END
                    ) ReceiveStatus
            FROM T_ApplicantDetails_Temp A
            WHERE isnull(A.bit_DeletedFlag, 0) = 0
                AND ISNULL(A.bit_ReceiveStatus, 0) = 1
                AND ISNULL(A.bit_Rejected, 0) = 1
                AND isnull(A.bit_DuplicateStatus, 0) = 0
                AND A.vch_AdvertisementNo = @Pvch_AdvertisementNo
                AND A.vch_Post_Code = @Pvch_Post_Code
                AND A.int_examDistrictId = @Pint_DistrictId
            ORDER BY A.vch_UniqueRefNo
        END
    END
END


取决于你想做什么。

请格式化你的问题!您可以使用{}按钮。很可能是在填充备注时使用的。如果运行填充@Comments变量的查询,它会显示多少条记录?
DECLARE @Remarks Varchar(MAX) = ''
SELECT   @Remarks = @Remarks + ',' + t.remarks
FROM     (
          SELECT DISTINCT
                  C.vch_Remarks AS remarks
          FROM    T_CAF_Receive C
                 ,T_ApplicantDetails_Temp A
          WHERE   C.vch_uniquerefno = A.vch_uniquerefno
                  AND ISNULL(c.bit_DeletedFlag, 0) = 0
         ) AS t
DECLARE @Remarks VARCHAR(max)
SET @Remarks = (
        SELECT TOP 1 distinct C.vch_Remarks
        FROM T_CAF_Receive C,T_ApplicantDetails_Temp A
        WHERE  C.vch_uniquerefno =A.vch_uniquerefno AND ISNULL(c.bit_DeletedFlag, 0) = 0
        )