문제

I want to get all values of column if the table valued parameter is null otherwise only the matched records. I have tried this but it gives an error when the subquery returns more than one record:

ALTER PROCEDURE [dbo].[usp_MY_SP]
(
    @TVP ABCTableType readonly,
)
AS

    SELECT *

    FROM TABLE t

    WHERE
        t.Id IN(
            CASE WHEN (SELECT COUNT([Id]) FROM @TVP) > 0 
                THEN (SELECT [Id] FROM @TVP)
                ELSE (t.Id)
            END
        )
도움이 되었습니까?

해결책

You can do it this way

ALTER PROCEDURE [dbo].[usp_MY_SP]
(
    @TVP ABCTableType readonly
)
AS

      SELECT * FROM t
      WHERE
          ((SELECT COUNT([Id]) FROM @TVP) = 0)
          OR
          t.Id IN (SELECT [Id] FROM @TVP)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top