stored proc - executing a query with NOT IN where clause

后端 未结 5 734
梦谈多话
梦谈多话 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: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)
    

提交回复
热议问题