SQL Stored Procedure LIKE

后端 未结 7 1891
野的像风
野的像风 2021-02-20 11:38

This is a simple question and I can\'t seem to think of a solution.

I have this defined in my stored procedure:

@communityDesc varchar(255) = NULL
         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-20 12:17

    at first you most create a function to split string some thing like this code

    CREATE FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )
    RETURNS
     @returnList TABLE ([Name] [nvarchar] (500))
    AS
    BEGIN
    
     DECLARE @name NVARCHAR(255)
     DECLARE @pos INT
    
     WHILE CHARINDEX(',', @stringToSplit) > 0
     BEGIN
      SELECT @pos  = CHARINDEX(',', @stringToSplit)  
      SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)
    
      INSERT INTO @returnList 
      SELECT @name
    
      SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
     END
    
     INSERT INTO @returnList
     SELECT @stringToSplit
    
     RETURN
    END
    

    then you can use this function is your query like this

    WHERE AREA IN (dbo.splitstring(@communityDesc))
    

提交回复
热议问题