简体   繁体   中英

Copy Table but with new Columns

In SQL Server, I have a table onto which new transaction data is loaded every day. I am trying to create a new table based on the existing table HOWEVER, in the new table I need to rename a column AND add a new concatenated column.

For example:

ExistingTable

    ID        Quantity    Name        Price    ReceiptNumber    Date
    1243      3           Stickers    5        4444             2016-12-01 
    4923      1           Glue        3        8288             2016-12-02
    1243      2           Stickers    5        1122             2016-12-04

From here, I want to rename the ID column to ItemID and then create an additional column which is a combination of the ID and ReceiptNumber columns

NewTable

    ItemID    Quantity    Name        Price    ReceiptNumber    Date          TransactionID
    1243      3           Stickers    5        4444             2016-12-01    1243-4444
    4923      1           Glue        3        8288             2016-12-02    4923-8288
    1243      2           Stickers    5        1122             2016-12-04    1243-1122

Notice the TransactionID is the combination of ItemID and ReceiptNumber. Essentially, I am trying to set it up so that I can load the data into this new table and then I will TRUNCATE the ExistingTable so that new data can be loaded there the next day. I can create the NewTable from scratch, but I'm not sure if it will map correctly to the new column names when I insert the new data. Any assistance would be much appreciated.

I would create the table first and then after that use the import\\export utility SQL Server has. When you right click on the DB it is under tasks. Then after you choose the source and destination you can choose to write your own SQL.

INSERT INTO <destination table>
(Columns)
SELECT <Columns to copy plus custom column> 
FROM <source table>

Then make sure the mapping is all correct and has the correct length requirements. After all that choose finish and it should load all the data into the table.

If you prefer though you can just have the job create the table for you but it will be an exact copy of the other table just the name will be different. You will need to change all the column names and create the custom column after the copy.

You could test that with something like this:

if exists (select * from tempdb.sys.objects where name like '#newtable%') begin; drop table #newtable; end;
if not exists (select * from tempdb.sys.objects where name like '#newtable%')
begin
create table #newtable (ItemID int,Quantity int, Name varchar(64), Price money, ReceiptNumber int, [Date] date, TransactionID varchar(32) ) 
end;

insert into #newtable (ItemId, Quantity, Name, Price, ReceiptNumber,[Date],TransactionID)
select ID, Quantity, Name, Price, ReceiptNumber, [Date], TransactionID = convert(varchar(14),ItemId)+'-'+(convert(varchar(14),ReceiptNumber))
from ExistingTable;

Assuming you have already created a table called NewTable , you can copy the data by using the following commands:

SET IDENTITY_INSERT NewTable ON

INSERT INTO NewTable (ItemID, Quantity, Name, Price, ReceiptNumber, Date, TransactionID)
       SELECT ID, Quantity, Name, Price, ReceiptNumber, Date, CONVERT(VARCHAR(14), ItemId) + '-' + (CONVERT(VARCHAR(14), ReceiptNumber) 
       FROM ExistingTable;

SET IDENTITY_INSERT NewTable OFF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM