将两个临时表与可变数量的条目组合在一起

时间:2015-11-20 09:13:54

标签: sql-server

我有一个关于两个不同表的条目组合的问题,其中一个表分别具有可变数量的条目填充列。

我的输入数据:

我有一个包含可变数量的条目/列的table1(我只知道有多少列可能是最大的),我有一个table2,其中有一个已定义的条目数,我为我的结果创建了一个table3,其中包含一个已定义的数字列数(列数=组合前两个表时的最大列数)。

我的任务是:

我想从table1中连续获取所有条目 - 并将其与table2中的一行中的四个条目组合 - 在新表3中的一行中。但与此同时,我不知道table1有多少填充列。因此,我无法定义table3的哪些列应插入table1的条目。

示例数据:

-- TABLE1 and TABLE2 are my input 
-- Here I defined 8 columns for TABLE1, but it can be more or less

CREATE TABLE #t1(
    [ID] [int] identity(1,1),
    [IDBG1] [int] NULL,
    [BG1] nvarchar(max),
    [IDBG2] [int] NULL,
    [BG2] nvarchar(max),
    [IDBG3] [int] NULL,
    [BG3] nvarchar(max),
    [IDBG4] [int] NULL,
    [BG4] nvarchar(max)
    )

CREATE TABLE #t2(
    [ID] [int] identity(1,1),
    [IDBG1] [int] NULL,
    [BG1] nvarchar(max),
    [IDBG2] [int] NULL,
    [BG2] nvarchar(max)
    )

-- TABLE3 is for my results 
-- number of columns is max. number of columns of TABLE 1 plus number of columns of TABLE2 
-- here: 8 Columns for TABLE1 entries and 4 columns for TABLE2 entries

CREATE TABLE #t3(
    [ID] [int] identity(1,1),
    [IDBG1] [int] NULL,
    [BG1] nvarchar(max),
    [IDBG2] [int] NULL,
    [BG2] nvarchar(max),
    [IDBG3] [int] NULL,
    [BG3] nvarchar(max),
    [IDBG4] [int] NULL,
    [BG4] nvarchar(max),
    [IDBG5] [int] NULL,
    [BG5] nvarchar(max),
    [IDBG6] [int] NULL,
    [BG6] nvarchar(max)
    )

-- the IDBG entries are ID's and the BG's are the corresponding names
-- for example

INSERT INTO #t1 (IDBG1, BG1, IDBG2, BG2, IDBG3, BG3, IDBG4, BG4)
VALUES (102, 'BS', 302, 'SL', 345, 'AS', 75, 'LT')

INSERT INTO #t2 (IDBG1, BG1, IDBG2, BG2)
VALUES (900, 'SM', 789, 'CS')

SELECT * FROM #t1
SELECT * FROM #t2
SELECT * FROM #t3

这可能吗?因为我只知道,必须在之前指定要填充的列!

1 个答案:

答案 0 :(得分:0)

你想做的事情叫做CROSS JOIN:

ID  IDBG1   BG1 IDBG2   BG2 IDBG3   BG3 IDBG4   BG4 IDBG5   BG5 IDBG6   BG6
1   102     BS  302     SL  345     AS  75      LT  900     SM  789     CS
2   103     BS  302     SL  345     AS  75      LT  900     SM  789     CS
3   102     BS  302     SL  345     AS  75      LT  901     SM  789     CS
4   103     BS  302     SL  345     AS  75      LT  901     SM  789     CS

输出:

declare @t1_id bigint = OBJECT_ID('tempdb.dbo.#t1');
declare @t2_id bigint = OBJECT_ID('tempdb.dbo.#t2');
declare @t3_id bigint = OBJECT_ID('tempdb.dbo.#t3');
declare @col_select nvarchar(max),  @col_insert nvarchar(max), @sql nvarchar(max);

-- Get #t1 columns
Set @col_select = STUFF((
        SELECT ', ' + 't1.' + QUOTENAME(name) 
        FROM  tempdb.sys.columns 
        WHERE OBJECT_ID = @t1_id AND name not like 'ID'
        ORDER BY column_id
        FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)') 
,1,1,'');

-- Add first 4 columns from #t2
Set @col_select = @col_select + ',' + STUFF((
        SELECT TOP(4) ', ' + 't2.' + QUOTENAME(name) 
        FROM  tempdb.sys.columns 
        WHERE OBJECT_ID = @t2_id AND name not like 'ID'
        ORDER BY column_id
        FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)') 
,1,1,'');

-- Get first n columns from #t3 (n = t1 count + 4)
Set @col_insert = STUFF((
        SELECT TOP(SELECT COUNT(*)-1+4 FROM  tempdb.sys.columns WHERE OBJECT_ID = @t1_id) ', ' + QUOTENAME(name) 
        FROM  tempdb.sys.columns 
        WHERE OBJECT_ID = @t3_id AND name not like 'ID'
        ORDER BY column_id
        FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)') 
,1,1,'');

SET @sql = '
    INSERT INTO #t3(' + @col_insert + ') 
    SELECT ' + @col_select + '
    FROM #t1 t1 CROSS JOIN #t2 t2
';

EXEC sp_executesql @sql;

请参阅:

现在,如果您不知道t1中的内容,则需要编写一些动态SQL。

此查询获取:

    @ li_select 中的
  • t1列(ID除外)
  • @col_select
  • 中#t2(ID除外)的前4列
  • @col_insert中来自#t3(ID除外)的前N列,其中N =来自t1 + 4的计数(对于t2)

查询:

$http.post('/downloadFile','', {responseType:'arraybuffer'}).success(function(res){.....}

用你的样本得到:

  • @col_select = t1。[IDBG1],t1。[BG1],t1。[IDBG2],t1。[BG2],t1。[IDBG3],t1。[BG3],t1。[IDBG4],t1。 [BG4],t2。[IDBG1],t2。[BG1],t2。[IDBG2],t2。[BG2]
  • @col_select = t1。[IDBG1],t1。[BG1],t1。[IDBG2],t1。[BG2],t1。[IDBG3],t1。[BG3],t1。[IDBG4],t1。 [BG4]
  • @col_insert = [IDBG1],[BG1],[IDBG2],[BG2],[IDBG3],[BG3],[IDBG4],[BG4],[IDBG5],[BG5],[IDBG6], [BG6]