SQL Stored Procedure LIKE

后端 未结 7 1890
野的像风
野的像风 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:37

    In only works with sets of values, not with characters in a string. To answer your question technically, the only way you could do this is to create a set of values representing the three values 'aaa', 'bbb' & 'ccc' and then put those three values into a table (a Temp Table or table variable (in SQL Server), and then perform IN against that set of values (against the table:

    declare @Vals table (value varchar(20))
    insert @vals(Value) Values('aaa')
    insert @vals(Value) Values('bbb')
    insert @vals(Value) Values('ccc')
    
    select * from SomeOtherTable 
    Where SomeColumn IN (Select value from @vals)
    

    To create the set you would need to create an empty temp table or table variable to hold this set of values, parse the comma delimited string into individual values, and enter those individual values into the temp table or table variable.

    although you don't say, if you are using SQL Server, the following is a SQL Server User Defined function (UDF) that will parse a delimited string and return a table with one row for each delimted value:

    if you create the UDF, then you would use it as follows:

    select * from SomeOtherTable 
    Where SomeColumn IN 
            (Select sVal from
              dbo.ParseSTring(@communityDesc, ','))
    

    /****** Object:  UserDefinedFunction [dbo].[ParseString]    
        Script Date:      4/8/2016 1:53:00 PM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER FUNCTION [dbo].[ParseString] (@S Text, @delim VarChar(5))
    Returns @tOut Table 
    (ValNum Integer Identity Primary Key, 
     sVal VarChar(8000))
    As
    Begin 
    Declare @dLLen TinyInt  -- Length of delimiter
    Declare @sWin  VarChar(8000)-- Will Contain Window into text string
    Declare @wLen  Integer  -- Length of Window
    Declare @wLast TinyInt  -- Boolean to indicate processing Last Window
    Declare @wPos  Integer  -- Start Position of Window within Text String
    Declare @sVal  VarChar(8000)-- String Data to insert into output Table
    Declare @BtchSiz Integer    -- Maximum Size of Window
    Set @BtchSiz = 7900 -- (Reset to smaller values to test routine)
    Declare @dPos Integer   -- Position within Window of next Delimiter
    Declare @Strt Integer   -- Start Position of each data value in Window
    -- --------------------------------------------------------------
    
    -- ---------------------------
    If @delim is Null Set @delim = '|'
    If DataLength(@S) = 0 Or
        Substring(@S, 1, @BtchSiz) = @delim Return
    -- ---------------------------
    Select @dLLen = Len(@delim),
        @Strt = 1, @wPos = 1,
        @sWin = Substring(@S, 1, @BtchSiz)
    Select @wLen = Len(@sWin),
          @wLast = Case When Len(@sWin) = @BtchSiz
                    Then 0 Else 1 End,
          @dPos = CharIndex(@delim, @sWin, @Strt)
    -- ----------------------------
    While @Strt <= @wLen
      Begin
        If @dPos = 0 Begin    -- No More delimiters in window
            If @wLast = 1 Set @dPos = @wLen + 1 
            Else Begin
                Set @wPos = @wPos + @Strt - 1
                Set @sWin = Substring(@S, @wPos, @BtchSiz)
                    -- -------------------------------------
                Select @wLen = Len(@sWin), @Strt = 1,
                @wLast = Case When Len(@sWin) = @BtchSiz
                    Then 0 Else 1 End, 
                                          @dPos = CharIndex(@delim, @sWin, 1)
                If @dPos = 0 Set @dPos = @wLen + 1 
                End
            End
            -- -------------------------------
        Set @sVal = LTrim(Substring(@sWin, @Strt, @dPos - @Strt))
        Insert @tOut (sVal) Values (@sVal)
        -- -------------------------------
        -- Move @Strt to char after last delimiter
        Set @Strt = @dPos + @dLLen 
        Set @dPos = CharIndex(@delim, @sWin, @Strt)
        End
    Return
     End
    
    0 讨论(0)
提交回复
热议问题