Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 组合两个具有可变条目数的临时表_Sql Server - Fatal编程技术网

Sql server 组合两个具有可变条目数的临时表

Sql server 组合两个具有可变条目数的临时表,sql-server,Sql Server,我有一个关于两个不同表的条目组合的问题,其中一个表的条目数可变,分别填充列 我的输入数据: -- 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),

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

我的输入数据:

-- 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(我只知道最大列数),我有一个定义条目数的表2,我为我的结果创建了一个定义列数的表3(列数=合并前两个表时的最大列数)

我的任务是:

-- 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中获取一行中的所有条目,并将其与新表3中的一行中的四个条目合并。但同时我不知道table1有多少填充列。因此,我无法定义表3的哪些列应该插入表1的条目

示例数据:

-- 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

可能吗?因为我只知道,应该填写的栏目必须事先指定

您要执行的操作称为交叉连接:

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

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

INSERT INTO #t3(IDBG1, BG1, IDBG2, BG2, IDBG3, BG3, IDBG4, BG4, IDBG5, BG5, IDBG6, BG6) 
SELECT t1.IDBG1, t1.BG1, t1.IDBG2, t1.BG2, t1.IDBG3, t1.BG3, t1.IDBG4, t1.BG4 
    , [IDBG5] = t2.IDBG1, [BG5] = t2.BG1, [IDBG6] = t2.IDBG2, [BG6] = t2.BG2
FROM #t1 t1 CROSS JOIN #t2 t2

SELECT * FROM #t1
SELECT * FROM #t2
SELECT * FROM #t3
输出:

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
见:

  • 在Technet上
  • 堆栈上溢出
现在,如果您不知道t1中是什么,那么需要编写一些动态SQL

此查询获取:

  • @col_select中的t1列(ID除外)
  • @col#u select中#t2(ID除外)的前4列
  • @col_insert中#t3(ID除外)的前N列,N=t1+4的计数(对于t2)
查询:

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;
通过您的示例,它可以得到:

  • @列select=t1.[IDBG1],t1.[IDBG1],t1.[IDBG2],t1.[IDBG3],t1.[IDBG3],t1.[IDBG4],t1.[BG4],t2.[IDBG1],t2.[IDBG2],t2.[BG2]
  • @列select=t1[IDBG1],t1[BG1],t1[IDBG2],t1[BG2],t1[IDBG3],t1[BG3],t1[IDBG4],t1[BG4]
  • @列插入=[IDBG1]、[BG1]、[IDBG2]、[IDBG3]、[IDBG4]、[BG4]、[IDBG5]、[BG5]、[IDBG6]、[BG6]

添加示例表数据和预期结果。听起来视图更适合我添加了一些示例数据。希望你现在能更好地理解。t1和t2之间没有关系吗?你只想让t2列在t1列的右边?如果表中有几行,预计会发生什么?更糟糕的是,行数不同?不,没有关系。我想将表1中的所有行与表2中的所有行合并,以便最终得到[(表1中的行数)乘以(表2中的行数)]不同的解决方案。