SQL插入查询

时间:2012-02-09 19:02:59

标签: sql sql-server

我有两个表,一个名为orders,另一个名为orders_archive。我有一些标记为“项目完成”的项目,我想进入orders表,但我遇到了问题。有人能指出我正确的方向吗?我的OrderNumber字段是一个标识种子,我插入的数字不在表中。我不知道这是否有所作为。

Insert INTO Orders VALUES (OrderNumber, [Church Name], CustomerID, [Date], ShippingAddress, ShippingCity, ShippingState, ShippingZip, ToBeShippedBY, Freight, Terms, SpecialInstructions, 
  SalesRep, OrderRecievedBY, PaymentType, PaymentAccount, PaymentEXPDate, PaymentCCV, PaymentNameonCard, PaymentBillingAdd, PaymentBillingCity, 
  PaymentBillingState, PaymentBillingZip, PaymentsameasChurchAdd, Item1NO, Item1Desc, Item1QTY, Item1Price, Item2NO, Item2Desc, Item2QTY, Item2Price, 
  Item3NO, Item3Desc, Item3QTY, Item3Price, Item4NO, Item4Desc, Item4QTY, Item4Price, Item5NO, Item5Desc, Item5QTY, Item5Price, Item6NO, Item6Desc, 
  Item6QTY, Item6PRice, Item7NO, Item7Desc, Item7QTY, Item7Price, Item8NO, Item8Desc, Item8QTY, Item8Price, [Office Approved], EmailContact, 
  ENTERontoSERVERFILE, PreparePO, SendPOtoManufacture, CopyTaxExempt, ScanDOCS, BusinessUnit, SalesTax, Notes, OrderStatus, AuthNUM, TaxExempt, 
  ItemDamaged, MiscNotes, StainColor, Framecolor, Silvervein, [Tracking Number], Deposit, SENT_Receipt, Prepared_PO, Sent_PO, Created_File_ON_Server, 
  Copy_Tax_forms_to_acctng, ChangefromPAYNENTPENDING, ScanMISCDocs, Process_Payment, ConfirmONServer, CheckINVENTORY, EnterIntoBooks, 
  PlacedIntoFilingCabinet, GSTOCK, DONTSHIPBEFOREDATE, Vendor, Item1weight, Item2weight, Item3weight, Item4weight, Item5weight, Item6weight, Item7weight, 
  Item8weight, TruckNumber, ShipDate, DeliverDate, Customer, ActualQuote, PersonQuotedActual, ShipName, HeaterSerialNum
FROM dbo.Orders_ARCHIVE
WHERE (OrderStatus LIKE 'Order Complete'))

2 个答案:

答案 0 :(得分:2)

将其更改为:

SET IDENTITY_INSERT Orders ON

INSERT INTO Orders
SELECT <columns>
FROM Orders_ARCHIVE
WHERE OrderStatus LIKE '%Order Complete%'

SET IDENTITY_INSERT Orders OFF

使用“VALUES”关键字禁止使用FROM和JOIN。

答案 1 :(得分:0)

您无法插入身份字段

在sql server上你可以启用标识插入,这将允许你插入到标识字段:     SET IDENTITY_INSERT Orders_ARCHIVE ON

然后再打开它     SET IDENTITY_INSERT Orders_ARCHIVE ON

虽然您可以创建重复的ID,但这不是一个很好的计划。如果您不需要存档ID,则只需从插入语句中删除OrderNumber字段。

Insert INTO Orders 
SELECT OrderNumber, [Church Name], CustomerID, [Date], ShippingAddress, ShippingCity, ShippingState, ShippingZip, ToBeShippedBY, Freight, Terms, SpecialInstructions, 
  SalesRep, OrderRecievedBY, PaymentType, PaymentAccount, PaymentEXPDate, PaymentCCV, PaymentNameonCard, PaymentBillingAdd, PaymentBillingCity, 
  PaymentBillingState, PaymentBillingZip, PaymentsameasChurchAdd, Item1NO, Item1Desc, Item1QTY, Item1Price, Item2NO, Item2Desc, Item2QTY, Item2Price, 
  Item3NO, Item3Desc, Item3QTY, Item3Price, Item4NO, Item4Desc, Item4QTY, Item4Price, Item5NO, Item5Desc, Item5QTY, Item5Price, Item6NO, Item6Desc, 
  Item6QTY, Item6PRice, Item7NO, Item7Desc, Item7QTY, Item7Price, Item8NO, Item8Desc, Item8QTY, Item8Price, [Office Approved], EmailContact, 
  ENTERontoSERVERFILE, PreparePO, SendPOtoManufacture, CopyTaxExempt, ScanDOCS, BusinessUnit, SalesTax, Notes, OrderStatus, AuthNUM, TaxExempt, 
  ItemDamaged, MiscNotes, StainColor, Framecolor, Silvervein, [Tracking Number], Deposit, SENT_Receipt, Prepared_PO, Sent_PO, Created_File_ON_Server, 
  Copy_Tax_forms_to_acctng, ChangefromPAYNENTPENDING, ScanMISCDocs, Process_Payment, ConfirmONServer, CheckINVENTORY, EnterIntoBooks, 
  PlacedIntoFilingCabinet, GSTOCK, DONTSHIPBEFOREDATE, Vendor, Item1weight, Item2weight, Item3weight, Item4weight, Item5weight, Item6weight, Item7weight, 
  Item8weight, TruckNumber, ShipDate, DeliverDate, Customer, ActualQuote, PersonQuotedActual, ShipName, HeaterSerialNum
FROM dbo.Orders_ARCHIVE
WHERE (OrderStatus LIKE 'Order Complete')

你的插入语句不正确你在做select时不使用values子句。