插入输入项的所有组合的最佳方式

时间:2015-06-21 07:32:50

标签: sql sql-server tsql

我有一张看起来像这样的表:

firstname    lastname      address      city
    a          b           (c,d,e)      (f,g,h)

我想制作一系列这样的插页:

insert (a,b,c,f)
insert (a,b,c,g)
insert (a,b,c,h)
insert (a,b,d,f)
insert (a,b,d,g)
insert (a,b,e,h)
....

在不使用多个游标的情况下,在SQL Server中执行此操作的最佳方法是什么?

2 个答案:

答案 0 :(得分:6)

只需在表格中使用cross apply,然后将一些字符串拆分为udf 您可以在this article.

中找到最适合您的一个

我已经使用了我链接到的文章中的SplitStrings_Moden函数来演示此示例:

CREATE FUNCTION dbo.SplitStrings_Moden
(
   @List NVARCHAR(MAX),
   @Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING AS
RETURN
  WITH E1(N)        AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 
                         UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 
                         UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
       E2(N)        AS (SELECT 1 FROM E1 a, E1 b),
       E4(N)        AS (SELECT 1 FROM E2 a, E2 b),
       E42(N)       AS (SELECT 1 FROM E4 a, E2 b),
       cteTally(N)  AS (SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(@List,1))) 
                         ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E42),
       cteStart(N1) AS (SELECT t.N+1 FROM cteTally t
                         WHERE (SUBSTRING(@List,t.N,1) = @Delimiter OR t.N = 0))
  SELECT Item = SUBSTRING(@List, s.N1, ISNULL(NULLIF(CHARINDEX(@Delimiter,@List,s.N1),0)-s.N1,8000))
    FROM cteStart s;

一旦你有了分割功能,你所要做的就是从你的表中选择值,并对你拥有的每个逗号分隔列使用交叉应用这个函数:

-- INSERT INTO MyTable(firstname, lastname, address, city)
SELECT firstname, lastname, a.item, b.item
FROM MyTable
cross apply dbo.SplitStrings_Moden([address], ',') a
cross apply dbo.SplitStrings_Moden(city, ',') b

See fiddle here

答案 1 :(得分:2)

您可以创建临时表并从笛卡尔联接中生成insert into ... select

create table #address(address varchar(1) not null)

insert into #address
values ('c'), ('d'), ('e')

create table #city(city varchar(1) not null)

insert into #city
values ('f'), ('g'), ('h')

insert into my_table
select 'a', 'b', address, city
from #address, #city

http://sqlfiddle.com/#!6/82b9a/2