TSQL Variable With List of Values for IN Clause

前端 未结 4 1299
攒了一身酷
攒了一身酷 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:55

    I solved this problem by using a CHARINDEX function. I wanted to pass the string in as a single parameter. I created a string with leading and trailing commas for each value I wanted to test for. Then I concatenated a leading and trailing commas to the string I wanted to see if was "in" the parameter. At the end I checked for CHARINDEX > 0

    DECLARE @CTSPST_Profit_Centers VARCHAR (256) 
    SELECT @CTSPST_Profit_Centers = ',CS5000U37Y,CS5000U48B,CS5000V68A,CS5000V69A,CS500IV69A,CS5000V70S,CS5000V79B,CS500IV79B,'
    
    SELECT 
       CASE
           WHEN CHARINDEX(','+ISMAT.PROFIT_CENTER+',' ,@CTSPST_Profit_Centers) > 0 THEN 'CTSPST'  
           ELSE ISMAT.DESIGN_ID  + ' 1 CPG' 
          END AS DESIGN_ID
    

    You can also do it in the where clause

    WHERE CHARINDEX(','+ISMAT.PROFIT_CENTER+',',@CTSPST_Profit_Centers) > 0
    

    If you were trying to compare numbers you'd need to convert the number to a text string for the CHARINDEX function to work.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-04 20:03

    Based on your update and assuming the lookup table is small, I suggest trying something like the following:

    DECLARE @MyLookup table
     (SomeValue nvarchar(100) not null)
    
    SELECT
       case when ml.SomeValue is not null then 1 else 0 end AS Priority   
      ,t.column_b
      ,t.column_c
     from MyTable t
      left outer join @MyLookup ml
       on ml.SomeValue = t.column_a
     order by case when ml.SomeValue is not null then 1 else 0 end desc
    

    (You can't reference the column alias "Priority" in the ORDER BY clause. Alternatively, you could use the ordinal position like so:

     order by 1 desc
    

    but that's generally not recommended.)

    As long as the lookup table is small , this really should run fairly quickly -- but your comment implies that it's a pretty big table, and that could slow down performance.

    As for n[Var]char vs. int, yes, integers would be faster, if only because the CPU has fewer bytes to juggle around... which shoud only be a problem when processing a lot of rows, so it might be worth trying.

    0 讨论(0)
  • 2021-01-04 20:14

    You can use a variable in an IN clause, but not in the way you're trying to do. For instance, you could do this:

    declare @i int
    declare @j int
    
    select @i = 10, @j = 20
    
    select * from YourTable where SomeColumn IN (@i, @j)
    

    The key is that the variables cannot represent more than one value.

    To answer your question, use the inline select. As long as you don't reference an outer value in the query (which could change the results on a per-row basis), the engine will not repeatedly select the same data from the table.

    0 讨论(0)
提交回复
热议问题