简体   繁体   English

将数据从表A插入表B

[英]Insert data from tableA to tableB

So I want to insert data from tableA to tableB depending on their pvkey. 因此,我想根据数据的pvkey将数据从tableA插入tableB。 Each row has his pvkey and one row under the same pvkey can have several records and each of these records has his unique cnkey. 每行都有他的pvkey,并且在同一pvkey下的一行可以有几条记录,并且每条记录都有他唯一的cnkey。 Cnkey is unique for any data in table. Cnkey对于表中的任何数据都是唯一的。 When I am inserting data from tableA there is no column Cnkey and in tableB there is Cnkey. 当我从tableA插入数据时,没有列Cnkey,而在tableB中,则有Cnkey。

INSERT INTO CONTHIST (CONTTYPE, ASSIGNEDTO, CONTDATE, SOURCE, CNKEY) 
    SELECT ContactType, ASSIGNEDTO, DATE, SourceCode, ?!?!  
    FROM MopUpEOC
    WHERE Pvkey in (1,5,7,9,11,20)

Can you help me with this, thank you :D 您能帮我吗,谢谢:D

A few things that could help you: 一些可以帮助您的事情:

  • Check if CnKey is the serial primary key of your table. 检查CnKey是否为表的串行主键。 If it is the serial primary key, you have no problem. 如果它是串行主键,则没有问题。
  • Check if CnKey is NOT NULL , if not, you can change it's value later, or you can set a default value. 检查CnKey是否为NOT NULL ,否则,可以稍后更改它的值,或者可以设置默认值。

For example, the column CnKey is NOT NULL , then you can use following Query to allow NULL : 例如,列CnKeyNOT NULL ,则可以使用以下Query来允许NULL

ALTER TABLE TableB ALTER COLUMN CnKey INT NULL

Change INT to the real type of your column. INT更改为您的列的实际类型。

Or you set a Default Value: 或者您设置默认值:

ALTER TABLE TableB ADD CONSTRAINT ConstraintName DEFAULT 'DefaultName' FOR CnKey;

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

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