2015-03-31 92 views
-2

我試圖通過逗號列表分隔的電子郵件ID的@ b.com,S @ b.com成在第SQL是我的查詢權,請給一些建議如何將逗號分隔列表發送到in子句中?

SELECT * FROM service.cs_list_of_email_ids where Email in (
     SELECT regexp_substr(:[email protected],[email protected],'[^,]+', 1, level) items 
     FROM dual 
     CONNECT BY regexp_substr(:[email protected],[email protected], '[^,]+', 1, level) is not null 
    ); 
+0

是嗎?你爲什麼問我們?它工作與否? :D – Luaan 2015-03-31 07:18:30

+0

http://stackoverflow.com/questions/19885076/how-to-split-string-and-insert-values-into-table-in-sql-server – 2015-03-31 07:18:56

回答

0

既然你沒有提到的如果你正在使用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,',')) 
相關問題