stored proc - executing a query with NOT IN where clause

后端 未结 5 689
梦谈多话
梦谈多话 2021-01-26 09:58

i have a stored procedure

Create PROCEDURE abc      
  @sRemovePreviouslySelectedWhereClause nvarchar(max)
AS
BEGIN

SELECT * 
      FROM table 
     WHERE nId          


        
相关标签:
5条回答
  • 2021-01-26 10:03

    You have to split the @sRemovePreviouslySelectedWhereClause parameter by ',' and place the resulting values in a temp table. Then your select would look like

    select * from table where nId not in (select nId from #tempIds)
    
    0 讨论(0)
  • 2021-01-26 10:05

    This approach you're trying doesn't work. But if you're on SQL Server 2008, you could make use of the new features called Table Valued Parameters.

    Basically, you create a user-defined table type

    CREATE TYPE dbo.nIdTable AS TABLE(nID INT)
    

    and you can then pass in multiple values in that TVP from the outside (e.g. from ADO.NET or such):

    CREATE PROCEDURE abc(@idValues dbo.nIdTable READONLY)
    

    and use that table variable inside your stored proc:

    SELECT * 
    FROM table 
    WHERE nId NOT IN (SELECT nID FROM @idValues)
    
    0 讨论(0)
  • 2021-01-26 10:06

    First, create a split function which splits your delimited string into a table:

    CREATE FUNCTION [dbo].[Split]
    (   
     @String varchar(max)
    ,@Delimiter char
    )
    RETURNS @Results table
    (
     Ordinal int
    ,StringValue varchar(max)
    )
    as
    begin
    
        set @String = isnull(@String,'')
        set @Delimiter = isnull(@Delimiter,'')
    
        declare
         @TempString varchar(max) = @String
        ,@Ordinal int = 0
        ,@CharIndex int = 0
    
        set @CharIndex = charindex(@Delimiter, @TempString)
        while @CharIndex != 0 begin     
            set @Ordinal += 1       
            insert @Results values
            (
             @Ordinal
            ,substring(@TempString, 0, @CharIndex)
            )       
            set @TempString = substring(@TempString, @CharIndex + 1, len(@TempString) - @CharIndex)     
            set @CharIndex = charindex(@Delimiter, @TempString)
        end
    
        if @TempString != '' begin
            set @Ordinal += 1 
            insert @Results values
            (
             @Ordinal
            ,@TempString
            )
        end
    
        return
    end
    

    Then change your where clause as follows:

    select
     t.*
    from [yourTable] t
    where t.[ID] not in (select cast([StringValue] as int) from dbo.Split(@sRemovePreviouslySelectedWhereClause,','))
    
    0 讨论(0)
  • 2021-01-26 10:21

    You will need to use Dynamic sql for such kind of queries.

    first construct the query and

    SET @sql = 'select * from table 
    where nId not in (' + @sRemovePreviouslySelectedWhereClause+ ')'
    

    then use EXEC(@sql) to run the query.

    0 讨论(0)
  • 2021-01-26 10:22
    Create FUNCTION [dbo].[fn_Split] (  
    @List nvarchar(2000),   @SplitOn nvarchar(5)
     )   
    RETURNS @RtnValue table  ( 
        Value nvarchar(100) ) 
     AS   
    BEGIN 
    While (Charindex(@SplitOn,@List)>0) 
    Begin  
    Insert Into @RtnValue (value)
     Select    Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) 
    Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List)) 
    End  
    Insert Into @RtnValue (Value)
        Select Value = ltrim(rtrim(@List))
    
        Return 
    END
    
    **********
    Create PROCEDURE abc      
      @sRemovePreviouslySelectedWhereClause nvarchar(max)
    AS
    BEGIN
    
    SELECT * 
          FROM Table 
         WHERE nId NOT IN (select * from dbo.fn_Split(@sRemovePreviouslySelectedWhereClause,','))
    
    END;
    
    0 讨论(0)
提交回复
热议问题