Mssql Insert multiple raw with same id from other column

时间:2017-05-16 09:32:16

标签: sql sql-server sql-insert

Hello i have a matter with an insert query ill try to explain bellow :

Here is a simplify view of my tables Tables

Table A         Table B
ID / Number   ID
1 / 0               1
1 / 1               2
1 / 2               3
2 / 0               4
2 / 1               5
2 / 2               6
3 / 0               7
3 / 1               8
3 / 2               9

so i already have my query who select the id of table B not inside table A

I want to insert them in Table A but as you can see i need to insert them 3 time and after the first insert my query dont work anymore.

What is the best way to do this? Create a view? a tamporary table? or multiple query?

So thx to all who have answer here is the solution i used to achieve the matter:

//Here i get the Missing ID and put them inside temp Table
select ID into #temp_id from TableA rv left outer join TableB lrva on
rv.ID = lrva.ID where 'other Table B attribut'  is null

//Then i insert the values
insert into TableA select ID, 1 from #temp_id;
insert into TableA select ID, 2 from #temp_id;
insert into TableA select ID, 3 from #temp_id;

//Then destroy the temp table
Drop Table #temp_id

Keep in mind that i need specific treatment for the Number column as for other column i dont name on the exemple that s why i cant just do a +1 or things like this. The meaning of the question was just how to insert in the easyest way multiple id from other table, and not really how to achieve this in the exemple who as simple data.

2 个答案:

答案 0 :(得分:0)

我猜这有效:

CREATE table TABLEA(id int, [number] int)
CREATE table TABLEB (id int)
insert into TABLEA values (1,  0 )
insert into TABLEA values (1,  1 )
insert into TABLEA values (1,  2 )
insert into TABLEA values (2,  0 )
insert into TABLEA values (2,  1 )
insert into TABLEA values (2,  2 )
insert into TABLEA values (3,  0 )
insert into TABLEA values (3,  1 )
insert into TABLEA values (3,  2 )

insert into TABLEB values (1)
insert into TABLEB values (2)
insert into TABLEB values (3)
insert into TABLEB values (4)
insert into TABLEB values (5)
insert into TABLEB values (6)
insert into TABLEB values (7)
insert into TABLEB values (8)
insert into TABLEB values (9)

DECLARE @QUERY VARCHAR(MAX)
DECLARE C CURSOR FOR
select 'INSERT INTO TABLEA VALUES ('+convert(char,id)+',0),('+convert(char,id)+',1),('+convert(char,id)+',2)' from TABLEB where id not in (select id from TABLEA)
OPEN C
FETCH NEXT FROM C INTO @QUERY
WHILE @@FETCH_STATUS=0
BEGIN
EXEC (@QUERY)
FETCH NEXT FROM C INTO @QUERY
END
CLOSE C
DEALLOCATE C

SELECT * FROM TABLEA

id          number
----------- -----------
1           0
1           1
1           2
2           0
2           1
2           2
3           0
3           1
3           2
4           0
4           1
4           2
5           0
5           1
5           2
6           0
6           1
6           2
7           0
7           1
7           2
8           0
8           1
8           2
9           0
9           1
9           2

答案 1 :(得分:0)

尝试这个:这会给你你想要的东西,它会产生与你的要求完全相同的产生笛卡尔值,你可以通过WHERE来限制数据来添加条件:

create table #tableA(id int, number int)
create table #tableB(id int)

insert into #tableB values
(1),(2),(3),(4),(5),(6),
(7),(8),(9)

insert into #tableA
select t.id, number
from (values(0),(1),(2)) x (number)
cross apply(select id from #tableB) t
where not exists(select 1 from #tableA A where A.id = t.id)

<强>输出      - 从#tableA

中选择*
id  number
1   0
1   1
1   2
2   0
2   1
2   2
3   0
3   1
3   2
--so on for each ids...