从电子邮件列表中分割10封电子邮件,其中包括30个,从10个到10个

时间:2011-10-31 03:39:45

标签: sql sql-server-2005 tsql

我的查询中有一个电子邮件列表,包含30多封电子邮件。

Print @Emails
  

结果:email1 [a] test.com; EMAIL2并[a] test.com; ......; email35 [α] test.com;

我需要从10发送到10.我必须从此列表中取10发送。

如何使用SQL Server完成此操作?

1 个答案:

答案 0 :(得分:0)

试试这个sql,它使用公用表表达式(CTE)来循环输入值,还有其他方法可用但是如果你想分解字符串输入并将其转换为表格,这应该满足你的需求: / p>

DECLARE @DemoTable table (Layout varchar(2000)) 
INSERT @DemoTable VALUES ('email1[a]test.com;email2[a]test.com;email3[a]test.com;email4[a]test.com;email5[a]test.com;email6[a]test.com;email7[a]test.com;email8[a]test.com;email9[a]test.com;email10[a]test.com;email11[a]test.com;email12[a]test.com;email13[a]test.com;email14[a]test.com;email15[a]test.com;email16[a]test.com;email17[a]test.com;email18[a]test.com;email19[a]test.com;email20[a]test.com;email21[a]test.com;email22[a]test.com;email23[a]test.com;email24[a]test.com;email25[a]test.com;email26[a]test.com;email27[a]test.com;email28[a]test.com;email29[a]test.com;email30[a]test.com;email31[a]test.com;email32[a]test.com;email33[a]test.com;email34[a]test.com;email35[a]test.com;') 

DECLARE @StringToTable table (email varchar(200)) 
DECLARE @Concatenated VARCHAR(2000)
SET @Concatenated=''
;WITH StringToTable AS 
(   SELECT LEFT(Layout,CHARINDEX(';',Layout)) AS Email 
        ,  RIGHT(Layout,LEN(Layout)-CHARINDEX(';',Layout)) StringToProcess 
    FROM @DemoTable 
    WHERE Layout IS NOT NULL AND CHARINDEX(';',Layout)>0 
    UNION ALL 
    SELECT LEFT(StringToProcess,CHARINDEX(';',StringToProcess)) 
        ,  RIGHT(StringToProcess,LEN(StringToProcess)-CHARINDEX(';',StringToProcess)) 
    FROM StringToTable 
    WHERE StringToProcess IS NOT NULL AND CHARINDEX(';',StringToProcess)>0 
) 
INSERT INTO @StringToTable(email)
SELECT TOP 10 Email FROM StringToTable--TOP 10 is to get the 10 mentioned in the question - you can change the order by to affect which 10 you wish to use.

SELECT Email  FROM @StringToTable

如果需要将行重新集成到字符串中,可以这样做:

SELECT @Concatenated = @Concatenated + Email  FROM @StringToTable

SELECT @Concatenated