SQL - using a variable for an IN clause

后端 未结 7 1390
醉话见心
醉话见心 2021-01-21 09:50

I wish to do something like the following:

declare @FrameNumber nvarchar(20)
set @FrameNumber = \'(p1, p2)\'

select from myTable where c1 in @FrameNumber
         


        
7条回答
  •  花落未央
    2021-01-21 09:59

    If you are using Sql Server 2005+ have a look at this

    --Split
    DECLARE @textXML XML
    DECLARE @data NVARCHAR(MAX), 
            @delimiter NVARCHAR(5)
    
    SELECT  @data = 'A,B,C',
            @delimiter = ','
    
    SELECT    @textXML = CAST('' + REPLACE(@data, @delimiter, '') + '' AS XML)
    SELECT  T.split.value('.', 'nvarchar(max)') AS data
    FROM    @textXML.nodes('/d') T(split)
    

    You can you that as your in table to select from.

    Have a look at XML Support in Microsoft SQL Server 2005

提交回复
热议问题