TSQL Variable With List of Values for IN Clause

前端 未结 4 1301
攒了一身酷
攒了一身酷 2021-01-04 19:27

I want to use a clause along the lines of \"CASE WHEN ... THEN 1 ELSE 0 END\" in a select statement. The tricky part is that I need it to work with \"value IN @List\".

4条回答
  •  佛祖请我去吃肉
    2021-01-04 19:59

    This might be along the lines of what you need.
    Note that this assumes that you have permissions and the input data has been sanitized.

    From Running Dynamic Stored Procedures

    CREATE PROCEDURE MyProc (@WHEREClause varchar(255))
    AS
    
        -- Create a variable @SQLStatement
        DECLARE @SQLStatement varchar(255)
    
        -- Enter the dynamic SQL statement into the
        -- variable @SQLStatement
        SELECT @SQLStatement = "SELECT * FROM TableName WHERE " + @WHEREClause
    
        -- Execute the SQL statement
        EXEC(@SQLStatement)
    

提交回复
热议问题