2016-04-14 2 views
0

Ich habe zwei Tabellen wie folgt aus:Insert Datensätze in einer Tabelle mit Schleife von einem anderen Tabellendaten

SupplyList (IDSupply ist der Primärschlüssel)

IDSupply PartName Qty 
--------- --------- ---- 
1   C   10 
2   B   4 

SupplyIndex (IDSupply und Index sind der zusammengesetzte Primärschlüssel)

IDSupply PartName Index 
--------- --------- ------ 
1   C  2 
1   C  3 
1   C  7 
1   C  9 
1   C  10 

Diese Tabellen sind miteinander verwandt mit IDSupply.

Ich möchte verpasste Datensätze in SupplyIndex Tabelle durch eine Abfrage in SQL einfügen. Mit anderen Worten, ist mein erwartetes Ergebnis SupplyIndex Tabelle unten wie (Index müssen Zahlen 1-Qty aus SupplyList Tabelle enthält)

IDSupply PartName Index 
--------- --------- ------ (result) 
1   C  1 
1   C  2 
1   C  3 
1   C  4 
1   C  5 
1   C  6 
1   C  7 
1   C  8 
1   C  9 
1   C  10 
2   B  1 
2   B  2 
2   B  3 
2   B  4 

ich diesen Job hätte in meiner VB.Net Anwendung vor und jetzt will ich es tun in SQL Server direkt.

Würden Sie mir bitte helfen?

Dank

Antwort

1

Testdaten:

create table #supplylist 
(
idsupply int, 
partname char(20), 
qty int 
) 

insert into #supplylist 
select 1,'a',10 
union all 
select 2,'c',4 

create table #idsupply 
(
idsupply int, 
partname char(20), 
indexx int 
) 

insert into #idsupply 
select 1,'a',10 
union all 
select 2,'c',3 

benutzte ich Numbers Tabelle dieses

with cte 
as 
(
select 
idsupply, 
partname,n 
from 
#supplylist t 
cross apply 
(
select n from numbers where n <=qty 
)b 

--final Teil zu erreichen, in anderen table..same Abfrage zu überprüfen und isnert wie oben mit einfügen und existiert

with cte 
as 
(
select 
idsupply, 
partname,n 
from 
#supplylist t 
cross apply 
(
select n from numbers where n <=qty 
)b 
) 
insert into #idsupply 
select * from cte t1 where not exists (select 1 from #idsupply t2 where t2.indexx=t1.n) 
+0

Vielen Dank für Ihre Antwort. es funktioniert nicht für mich. Bitte überprüfen Sie auch meine Eingabe in der SupplyIndex-Tabelle. Ich brauche Moorhilfe. Danke –

+0

Können Sie weitere Informationen zur Verfügung stellen. Sie müssen Ihre Tabellennamen ersetzen und Nummern installieren Tabelle – TheGameiswar

0
Create Table #SupplyList(IDSupply int Primary Key, PartName char(10),Qty int); 
Insert #SupplyList(IDSupply, PartName, Qty) Values 
(1,   'C',   10), 
(2,   'B',   4); 
Create Table #SupplyIndex(IDSupply int, PartName char(10), [Index] int Primary Key (IdSupply, [Index])); 
Insert #SupplyIndex(IDSupply, PartName, [Index]) Values 
(1,   'C',  2), 
(1,   'C',  3), 
(1,   'C',  7), 
(1,   'C',  9), 
(1,   'C',  10); 
;With cteMax As 
(Select Max([Index]) As MaxIndex From #SupplyIndex), 
cteNumbers As 
(Select 1 As Number 
Union All 
Select Number + 1 
From cteNumbers n 
Cross Join cteMax m 
Where n.Number < m.MaxIndex) 
Merge #SupplyIndex t 
Using (Select sl.IDSupply, sl.PartName, n.Number As Qty From #SupplyList sl Inner Join cteNumbers n On n.Number <= sl.Qty) s 
On s.IDSupply = t.IDSupply And s.PartName = t.PartName And s.Qty = t.[Index] 
When Not Matched Then Insert(IDSupply, PartName, [Index]) Values(s.IDSupply, s.PartName, s.Qty); 

Select * From #SupplyIndex; 


go 
Drop Table #SupplyList; 
go 
Drop Table #SupplyIndex 
+0

Tom Cooper von MSDN lösen es. –

Verwandte Themen