I have a question concerning the combination of entries of two different tables, where one of them has a variable number of entries respectively filled columns.
My input data:
I have a table1 containing a variable number of entries/columns (I only know how many columns there could be maximal), I have a table2 with a defined number of entries and I created a table3 for my results with a defined number of columns (number of columns = maximum number of columns when combining the first two tables).
My task is:
I want to take - all entries in a row from table1 - and combine it with only - four entries in a row from table2 - in one row in a new table3. But at the same time I don't know how many filled columns table1 has. So I can't define in which columns of table3 the entries of table1 should be inserted.
Example data:
-- 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
Is that possible? Because I only know, that the columns which should be filled have to be specified before!