【问题标题】:how to send a comma separated list into in clause?如何将逗号分隔的列表发送到 in 子句?
【发布时间】:2015-03-31 07:14:47
【问题描述】:

我正在尝试将逗号分隔的电子邮件 ID a@b.com,s@b.com 的列表传递到 sql in 子句中,这是我的查询权,请提供一些建议

 SELECT * FROM service.cs_list_of_email_ids where Email in (
        SELECT regexp_substr(:a@b.com,s@b.com,'[^,]+', 1, level) items
        FROM dual
        CONNECT BY regexp_substr(:a@b.com,s@b.com, '[^,]+', 1, level) is not null
    );

【问题讨论】:

标签: sql database csv


【解决方案1】:

既然你没有提到你是否使用Sql Server,那么你可以创建这个函数来分割逗号分隔的字符串

CREATE FUNCTION Split
(
  @delimited nvarchar(max),
  @delimiter nvarchar(100)
) RETURNS @t TABLE
(
-- Id column can be commented out, not required for sql splitting string
  id int identity(1,1), -- I use this column for numbering splitted parts
  val nvarchar(max)
)
AS
BEGIN
  declare @xml xml
  set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

  insert into @t(val)
  select
    r.value('.','varchar(max)') as item
  from @xml.nodes('//root/r') as records(r)

  RETURN
END
GO

然后如下调用它

SELECT * FROM service.cs_list_of_email_ids where Email in 
       (SELECT val FROM dbo.split(@str,','))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 2016-01-04
    • 2013-08-03
    • 2013-06-04
    • 1970-01-01
    相关资源
    最近更新 更多