SQL Server Getting error - Update where clause based on if -else condition

qojgxg4l  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(91)

I have below query to select set of record based on the ccmflag condition.

CREATE OR ALTER PROCEDURE [dbo].[getdata]
    @p_ccmflag BIT = 0
AS
    SELECT *
    FROM dbo.data

    IF @p_ccmflag = 1
    BEGIN
        WHERE CCMFlag = 1
    END
    ELSE
    BEGIN
        WHERE CCMFlag != 1
    END
    ORDER BY 1 ASC;

I am getting this error:

Msg 156, Level 15, State 1
Incorrect syntax near the keyword 'where'.

pw9qyyiw

pw9qyyiw1#

Earlier someone posted an answer, I tried that query and its working fine for me.

Query as below:

SELECT *
    FROM dbo.data
    WHERE COALESCE(CCMFlag,0) = @p_ccmflag
    ORDER BY 1 ASC;

相关问题