0

我目前正在尝试将所有数据从一个表复制到另一个表,因为第二个表的排序有点不同我正在使用以下 SQL 脚本来执行此操作:

USE LoanersTest
DROP TABLE LoanerItems1
CREATE TABLE LoanerItems1
(
LoanerKey INT NOT NULL UNIQUE 
,BCPartNumber VARCHAR(255)  NULL 
,Manufacturer VARCHAR(255)  NULL 
,Model VARCHAR(255)  NULL 
,Modality INT NOT NULL UNIQUE
,Status INT  NOT NULL UNIQUE
,Location INT  NOT NULL UNIQUE
,ShelvingKey INT NOT NULL UNIQUE
,CalDate VARCHAR(255)  NULL 
,CalDue VARCHAR(255)  NULL 
,BCID VARCHAR(255)  NULL 
,SerialNumber VARCHAR(255)  NULL 
,AssetTag VARCHAR(255)  NULL 
,LoanTerm VARCHAR(255)  NULL 
,PartNumber VARCHAR(255)  NULL 
,Notes VARCHAR(255)  NULL 
,CriticalNotes VARCHAR(255)  NULL 
,ShippingNotes VARCHAR(255)  NULL 
,ReceivingNotes VARCHAR(255)  NULL 
,Description VARCHAR(255)  NULL 
,Details VARCHAR(255)  NULL 
,Attachments VARCHAR(255)  NULL 
,Manuals VARCHAR(255)  NULL 
,PRIMARY KEY (LoanerKey) 
)
INSERT INTO LoanerItems1 (LoanerKey)
  SELECT LoanerKey FROM LoanerItems

INSERT INTO LoanerItems1 (BCPartNumber)
  SELECT BCPartNumber FROM LoanerItems

INSERT INTO LoanerItems1 (Manufacturer)
  SELECT Manufacturer FROM LoanerItems

INSERT INTO LoanerItems1 (Model)
  SELECT Model FROM LoanerItems

INSERT INTO LoanerItems1 (Modality)
  SELECT Modality FROM LoanerItems

INSERT INTO LoanerItems1 (Status)
  SELECT Status FROM LoanerItems

INSERT INTO LoanerItems1 (Location)
  SELECT Location FROM LoanerItems

INSERT INTO LoanerItems1 (ShelvingKey)
  SELECT ShelvingKey FROM LoanerItems

INSERT INTO LoanerItems1 (CalDate)
  SELECT CalDate FROM LoanerItems

INSERT INTO LoanerItems1 (CalDue)
  SELECT CalDue FROM LoanerItems

INSERT INTO LoanerItems1 (BCID)
  SELECT BCID FROM LoanerItems

INSERT INTO LoanerItems1 (SerialNumber)
  SELECT SerialNumber FROM LoanerItems

INSERT INTO LoanerItems1 (AssetTag)
  SELECT AssetTag FROM LoanerItems

INSERT INTO LoanerItems1 (LoanTerm)
  SELECT LoanTerm FROM LoanerItems

INSERT INTO LoanerItems1 (PartNumber)
  SELECT PartNumber FROM LoanerItems

INSERT INTO LoanerItems1 (Notes)
  SELECT Notes FROM LoanerItems

INSERT INTO LoanerItems1 (CriticalNotes)
  SELECT CriticalNotes FROM LoanerItems

INSERT INTO LoanerItems1 (ShippingNotes)
  SELECT ShippingNotes FROM LoanerItems

INSERT INTO LoanerItems1 (ReceivingNotes)
  SELECT ReveivingNotes FROM LoanerItems

INSERT INTO LoanerItems1 (Description)
  SELECT Description FROM LoanerItems

INSERT INTO LoanerItems1 (Details)
  SELECT Details FROM LoanerItems

INSERT INTO LoanerItems1 (Attachments)
  SELECT Attachments FROM LoanerItems

INSERT INTO LoanerItems1 (Manuals)
  SELECT Manuals FROM LoanerItems

第一个表的格式完全相同(尽管列的顺序略有不同)并且包含大约 80 条记录。没有任何记录包含空值,但执行此语句会返回以下内容:

无法将值 NULL 插入到列 'Modality'、表 'LoanersTest.dbo.LoanerItems1' 中;列不允许空值。插入失败。

无法将值 NULL 插入到列“LoanerKey”、表“LoanersTest.dbo.LoanerItems1”中;列不允许空值。插入失败。

中的值LoanerKey基本上是 1, 2, 3, 4, 5... 85, 86, 87。modality 列暂时包含相同的值。同样,两列都不包含空值,但仍返回此错误。

4

2 回答 2

5

不要插入单个列。在一个查询中插入所有列。

INSERT INTO LoanerItems1 (LoanerKey, BCPartNumber, Manufacturer...)
SELECT LoanerKey, BCPartNumber, Manufacturer, ....  FROM LoanerItems

当您只插入一列时,其他列将默认为 NULL,这就是您看到错误的原因。

于 2013-06-14T15:33:50.340 回答
0

您需要一次插入所有记录。您的语句试图插入一整行,但只设置一列。你要这个:

insert into LoanerItems1
(LoanerKey
,BCPartNumber
,Manufacturer
,Model
,Modality
,Status
,Location
,ShelvingKey
,CalDate
,CalDue
,BCID
,SerialNumber
,AssetTag
,LoanTerm
,PartNumber
,Notes
,CriticalNotes
,ShippingNotes
,ReceivingNotes
,Description
,Details
,Attachments
,Manuals
,BCPartNumber
,Manufacturer
,Model
,Modality
,Status
,Location
,ShelvingKey
,CalDate
,CalDue
,BCID
,SerialNumber
,AssetTag
,LoanTerm
,PartNumber
,Notes
,CriticalNotes
,ShippingNotes
,ReceivingNotes
,Description
,Details
,Attachments
,Manuals)
SELECT LoanerKey
,BCPartNumber
,Manufacturer
,Model
,Modality
,Status
,Location
,ShelvingKey
,CalDate
,CalDue
,BCID
,SerialNumber
,AssetTag
,LoanTerm
,PartNumber
,Notes
,CriticalNotes
,ShippingNotes
,ReceivingNotes
,Description
,Details
,Attachments
,Manuals
,BCPartNumber
,Manufacturer
,Model
,Modality
,Status
,Location
,ShelvingKey
,CalDate
,CalDue
,BCID
,SerialNumber
,AssetTag
,LoanTerm
,PartNumber
,Notes
,CriticalNotes
,ShippingNotes
,ReceivingNotes
,Description
,Details
,Attachments
,Manuals
from LoanerItems
于 2013-06-14T15:34:26.863 回答