0
的數據將是這樣的:迴路選擇成選擇與光標
表1的模式:表2的
+-----+--+
|Name |Id|
+-----+--+
|Alias|1 |
+-----+--+
|adam |2 |
+-----+--+
架構:
+-----+--+
|order|id|
+-----+--+
|ord1 |1 |
+-----+--+
|ord2 |1 |
+-----+--+
|ord3 |1 |
+-----+--+
|ord4 |2 |
+-----+--+
|ord5 |2 |
+-----+--+
我想要的結果是這樣的:
+-----+-+
|Alias|1|
+-----+-+
|ord1 |1|
+-----+-+
|ord2 |1|
+-----+-+
|ord3 |1|
+-----+-+
|adam |2|
+-----+-+
|ord4 |2|
+-----+-+
|ord5 |2|
+-----+-+
我用光標試過這個:
DECLARE @name, @id, @order
DECLARE curs_Fp CURSOR FOR
SELECT c.name, c.id, o.order FROM customer c INNER JOIN ORDER o ON c.id = o.id
OPEN curs_Fp
FETCH NEXT FROM curs_Fp INTO @name, @id, @order
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @name, @id
--here I spose to do something for loopping every orders to a customer where c.id = o.id
SELECT @order, @id
FETCH NEXT FROM curs_Fp INTO @name, @id, @order
END
CLOSE curs_Fp
DEALLOCATE curs_Fp