In the past, I have a database which containt a table like this:
ProductID | ProductName | InStockWareHouse1 | InstockWareHouse2
---------------------------------------------------------------
1 Name1 5 10
2 Name2 10 20
But now, in order for normal user to be able to create more WareHouse themselves, I have to change in to details-table. Like this:
Product table:
ProductID | ProductName
-----------------------
1 Name1
2 Name2
3 Name3
WareHouse table:
WareHouseID | WareHouseName
---------------------------
1 WareHouse1
2 WareHouse2
3 WareHouse3
WareHouseDetails table:
ProductID | WareHouseID | InStock
---------------------------------
1 2 5
2 2 10
2 3 10
Moving data from old Product table to new Product table wasn't hard, but I have spent one hour and still haven't come up with a solution to move the data from the old table to the new tables. Any suggestion ? I am fine with whether the solution is using query or write my self a program to read all the data and write back to the server, as long as I don't have to re-enter all the data by manually.
2条答案
按热度按时间zysjyyx41#
Assuming that your product ID and warehouse are auto-increment ints I would simply write a small script to do this by first copying your products to the products table, then manually creating your warehouse. Finally copy the products from the old product id table. Note we need to use
SET IDENTITY_INSERT <table> ON\OFF
in order to write to the auto increment key.Something similar to (note i used OldProducts as the name of the old products table)
oknrviil2#
You could use
INSERT INTO
to insert data from one table into another.To insert into
Product
:To insert into
WareHouse
:To insert into
WareHouseDetails
: