SQL Server Database: How to move data from non details-table to details-table?

6tdlim6h  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(113)

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.

zysjyyx4

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)

SET IDENTITY_INSERT Product ON
INSERT INTO Product (ProdcutId, ProductName) SELECT ProductId, ProductName FROM OldProducts
SET IDENTITY_INSERT Product OFF

SET IDENTITY_INSERT Warehouse ON
INSERT INTO Warehouse (WarehouseId, WarehouseName) VALUES (1, 'Warehouse1')
INSERT INTO Warehouse (WarehouseId, WarehouseName) VALUES (2, 'Warehouse2')
SET IDENTITY_INSERT Warehouse OFF

INSERT INTO WarehouseDetails (ProductId, WareHouseId, InStock) SELECT ProductId, 1, InStockWarehouse1 FROM OldProducts
INSERT INTO WarehouseDetails (ProductId, WareHouseId, InStock) SELECT ProductId, 2, InStockWarehouse2 FROM OldProducts
GO
oknrviil

oknrviil2#

You could use INSERT INTO to insert data from one table into another.

To insert into Product :

INSERT INTO Product(ProductID, ProductName)
    SELECT ProductID, ProductName FROM OldTable

To insert into WareHouse :

INSERT INTO WareHouse(WareHouseID, WareHouseName)
    SELECT 1, 'WareHouse1' UNION ALL
    SELECT 2, 'WareHouse2'

To insert into WareHouseDetails :

INSERT INTO WareHouseDetails(ProductID, WareHouseID, InStock)
    SELECT 
        ProductID,
        WareHouseID = 1,
        InStock = InStockWareHouse1
    FROM OldTable

    UNION ALL

    SELECT 
        ProductID,
        WareHouseID = 2,
        InStock = InStockWareHouse2
    FROM OldTable

相关问题