简体   繁体   English

复制表但有新列

[英]Copy Table but with new Columns

In SQL Server, I have a table onto which new transaction data is loaded every day. 在SQL Server中,我有一个表格,每天在该表格上加载新的交易数据。 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 在这里,我想将ID列重命名为ItemID,然后创建一个附加列,该列是ID和ReceiptNumber列的组合

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. 注意,TransactionID是ItemID和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. 本质上,我正在尝试进行设置,以便可以将数据加载到此新表中,然后将TRUNCATE EXISTINGTABLE,以便第二天可以将新数据加载到该表中。 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. 我可以从头开始创建NewTable,但是不确定在插入新数据时它是否可以正确映射到新列名。 Any assistance would be much appreciated. 任何帮助将不胜感激。

I would create the table first and then after that use the import\\export utility SQL Server has. 我将首先创建表,然后再使用SQL Server的import \\ export实用程序。 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. 然后,在选择源和目标之后,您可以选择编写自己的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: 假设您已经创建了一个名为NewTable的表,则可以使用以下命令复制数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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