如何在不使用所有Account-ID值的循環的情況下有效插入多行?爲每個賬戶ID插入多個?
INSERT INTO Table1
(AccountID, ShowColumns, GroupColumns, AvgColumnsFlag)
VALUES
(1, 'foo1', 'foo2', 'foo3')
(1, 'abc1', 'abc2', 'abc3')
(1, 'xyz1', 'xyz1', 'xyz1')
在這種情況下,我有超過20,000帳號ids。我可以使用另一個具有唯一帳戶ID的表格,並進行某種連接來獲得該帳戶。然後用它來代替顯示的示例Account-ID「1」。
我不知道你們如何處理每個帳戶ID的多個插入。
謝謝...
[編輯]
我找到了一種方法來插入使用來自其他表數據最近可惜我只能插入1行,而不是多行。 :-(請參見下面的代碼...是否可以鞏固它們的3到1,而不是
INSERT INTO tblDealerSavedDataMyInventorySavedBuilds
(AccountId, LoadDefault, BuildName, ColumnShowAndSortOrderValues, ColumnGroupByValues, ColumnSortAverageValues)
SELECT DISTINCT tblaAccounts.AccountID, 0, 'My Inventory by Count', 'ImportStatus|StockNumber|Vin|Year|Make ASC|Model ASC|Trim|Mileage|PurchasePrice|StockDate|RepairCost|TotalCost|DaysInInventory|InventoryTrackerLocation|Category', 'Make|Model', 'MyInventoryCount-SortOrderByCount'
FROM tblaAccounts
ORDER BY tblaAccounts.AccountID ASC
INSERT INTO tblDealerSavedDataMyInventorySavedBuilds
(AccountId, LoadDefault, BuildName, ColumnShowAndSortOrderValues, ColumnGroupByValues, ColumnSortAverageValues)
SELECT DISTINCT tblaAccounts.AccountID, 0, 'My Inventory by Make', 'ImportStatus|StockNumber|Vin|Year|Make ASC|Model ASC|Trim|Mileage|PurchasePrice|StockDate|RepairCost|TotalCost|DaysInInventory|InventoryTrackerLocation|Category', 'Make|Model', 'MyInventoryCount-SortOrderByMake'
FROM tblaAccounts
ORDER BY tblaAccounts.AccountID ASC
INSERT INTO tblDealerSavedDataMyInventorySavedBuilds
(AccountId, LoadDefault, BuildName, ColumnShowAndSortOrderValues, ColumnGroupByValues, ColumnSortAverageValues)
SELECT DISTINCT tblaAccounts.AccountID, 0, 'My Inventory by Purchase Price', 'ImportStatus|StockNumber|Vin|Year|Make ASC|Model ASC|Trim|Mileage|PurchasePrice|StockDate|RepairCost|TotalCost|DaysInInventory|InventoryTrackerLocation|Category', 'Make|Model', 'MyInventoryCount-SortOrderByCost'
FROM tblaAccounts
ORDER BY tblaAccounts.AccountID ASC
插入問題不是很清楚,數據來自哪裏?我想你想要的是[Insert with Select](http://www.sqlteam.com/article/using-select-to-insert-records) –
我認爲你的解決方案將是INSERT INTO TABLE1 SELECT .. '但是你能發表一些關於你如何使用其他表格的例子嗎? –
什麼表存儲您的20,000個帳戶ID?你只需要這三行的每個帳戶ID,所以總共插入了60,000行?什麼版本的SQL Server? –