通过从另一个表中进行选择将数据插入到多个表中

时间:2015-08-20 14:52:25

标签: sql sql-server-2012

  

我有3个表,想要在前2个表上插入记录   table3上的记录数。

     

表2中的My Table1_ID列与table1 ID链接。

     

table3中有8条记录,因此需要在其中插入这8条记录   table2 Name列,同时我需要向table1插入数据。   在table1中,如果您注意到位置在0中的fieldID 50开始   结果表

表1

|ID|FieldID|Position|
|0 |   0   |   0    |
|1 |   15  |   1    |
|2 |   15  |   2    |

表2

|ID|Table1_ID|  Y/N   | Name |
|0 |   0     |   1    | N1   |
|1 |   1     |   1    | N2   |
|2 |   2     |   1    | N3   |

表3

|ID| Name | 
|1 |      |
|2 | N4   |
|3 | N5   | 
|4 | N6   |
|5 | N7   |
|6 | N8   | 
|7 | N9   |
|8 | N10  |
|9 | N11  |

结果

表1

|ID|FieldID|Position|
|0 |   0   |   0    |
|1 |   15  |   0    |
|2 |   15  |   1    |
|3 |   50  |   0    |
|4 |   50  |   1    |
|5 |   50  |   2    |
|6 |   50  |   3    |
|7 |   50  |   4    |
|8 |   50  |   5    |
|9 |   50  |   6    |
|10|   50  |   7    |

表2

|ID|Table1_ID|  Y/N   | Name |
|0 |   0     |   1    | N1   |
|1 |   1     |   1    | N2   |
|2 |   2     |   1    | N3   |
|3 |   3     |   1    | N4   |
|4 |   4     |   1    | N5   |
|5 |   5     |   1    | N6   |
|6 |   6     |   1    | N7   |
|7 |   7     |   1    | N8   |
|8 |   8     |   1    | N9   |
|9 |   9     |   1    | N10  |
|10|   10    |   1    | N11  |

2 个答案:

答案 0 :(得分:0)

如果我理解正确的问题是如何一起插入两个表,那么:

1)打开一个事务,执行两个插入,然后提交数据更改。就任何其他会议而言,两个表都将一起更新。

2)使用sql-server中的OUTPUT选项。在插入table2时,添加OUTPUT选项以将数据插入table1。请参见链接https://msdn.microsoft.com/en-us/library/ms177564.aspx

答案 1 :(得分:0)

这绝对不是获得所需输出的最佳方法,但您可以执行以下操作(NOT RECOMMENDED):

创建表:

create table table1 (ID int identity(0,1), 
                     FieldID int, 
                     Position int
               CONSTRAINT pk_table1_id PRIMARY KEY(ID));

insert into table1 values
(0 ,0),
(15,1),
(15,2);

create table table2 (ID int identity(0,1), 
                     Table1_ID int,  
                     Y_N  int, 
                     Name varchar(50) 
              CONSTRAINT fk_table1_pid FOREIGN KEY(Table1_ID)REFERENCES table1(ID));

insert into table2 values
(0, 1, 'N1'),
(1, 1, 'N2'),
(2, 1, 'N3');

create table table3 (ID int, Name varchar(50));

insert into table3 values
(1,  null),  
(2, 'N4'),
(3, 'N5'), 
(4, 'N6'),
(5, 'N7'),
(6, 'N8'), 
(7, 'N9'),
(8, 'N10'),
(9, 'N11');


insert into table1 (FieldID,Position)
select 50 as FieldID,
       1 as Position
from table3
where name is not null;

insert into table2 (Table1_ID,Y_N,Name)
select [id] + 1 as Table1_ID,
       1 as Y_N,
       Name
from table3
where name is not null;

表1中的更新位置:

UPDATE t
SET position = t1.rn
FROM table1 t
INNER JOIN (
    SELECT  id,
            row_number() OVER (
            PARTITION BY FieldID ORDER BY ID
            ) - 1 AS rn
    FROM table1 
    ) t1 ON t.id = t1.id;

SELECT FROM TABLES:

select * from table1;

select * from table2;

<强>结果:

<强>表1

+----+---------+----------+
| ID | FieldID | Position |
+----+---------+----------+
|  0 |       0 |        0 |
|  1 |      15 |        0 |
|  2 |      15 |        1 |
|  3 |      50 |        0 |
|  4 |      50 |        1 |
|  5 |      50 |        2 |
|  6 |      50 |        3 |
|  7 |      50 |        4 |
|  8 |      50 |        5 |
|  9 |      50 |        6 |
| 10 |      50 |        7 |
+----+---------+----------+

<强>表2

+----+-----------+-----+------+
| ID | Table1_ID | Y_N | Name |
+----+-----------+-----+------+
|  0 |         0 |   1 | N1   |
|  1 |         1 |   1 | N2   |
|  2 |         2 |   1 | N3   |
|  3 |         3 |   1 | N4   |
|  4 |         4 |   1 | N5   |
|  5 |         5 |   1 | N6   |
|  6 |         6 |   1 | N7   |
|  7 |         7 |   1 | N8   |
|  8 |         8 |   1 | N9   |
|  9 |         9 |   1 | N10  |
| 10 |        10 |   1 | N11  |
+----+-----------+-----+------+

SQL Fiddle Demo

同样,这将适用于上面的输出,但这里有太多的设计缺陷。因此,更新后的原因和所有额外的东西。希望你可以重新设计你的桌子结构,这样你就可以避免所有的麻烦。