SQL Server查询,存储过程多个Identity_Insert表创建存档记录

时间:2013-07-20 02:45:19

标签: sql database sql-server-2008 stored-procedures identity-insert

BEGIN
IF @TransType = 1
BEGIN
       SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryHeaderHistory] ON 

       -- INSERT TO DELETE DELIVERY HEADER HISTORY
       INSERT INTO @DRHeaderHist (RowID, CompanyName, ItemClassCode, DeliveryDate,
                                  LoadingDate, DRNumber, Custnmbr, CustName,    
                                  TruckerID, PlateNumberID, TonnerID, DeliveryTypeID,
                                  Remarks, DateCreated, DateModified, DatePosted, 
                                  UserName, FGUserName, DRUserName, PostedFG, 
                                  Import, CareOf, CareOfName )
           SELECT * 
           FROM [Sales.DeliveryHeaderHistory] 
           WHERE DRNUMBER = @DR

       INSERT INTO dbo.Sales.DeletedDeliveryHeaderHistory (RowID, CompanyName, 
                       ItemClassCode,  DeliveryDate, LoadingDate, DRNumber, Custnmbr, 
                       CustName, TruckerID, PlateNumberID, TonnerID, DeliveryTypeID,
                       Remarks, DateCreated, DateModified, DatePosted, UserName, 
                       FGUserName, DRUserName, PostedFG, Import, CareOf, CareOfName )
           SELECT * 
           FROM @DRHeaderHist

SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryHeaderHistory] OFF 
SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryDetailsHistory] ON 

-- INSERT TO DELETE DELIVERY DETAIL HISTORY

INSERT INTO @DRDetailsHist ( RowID, CompanyName, DRNumber, OrderDocNum, OFLineNumber, LineNumber, Quantity, UofM, KgsPerBag, Itemnmbr,
                                                  TotalKgs, ItemDesc, VariantCode, VariantDesc, ProductionCode, OrderDate, DateNeeded, Week, Month1, PostedFG )
SELECT * from [dbo].[Sales.DeliveryDetailsHistory] WHERE DRNUMBER = @DR


INSERT INTO [dbo].[Sales.DeletedDeliveryDetailsHistory] ( RowID, CompanyName, DRNumber, OrderDocNum, OFLineNumber, LineNumber, Quantity, UofM, KgsPerBag, Itemnmbr,
                                                  TotalKgs, ItemDesc, VariantCode, VariantDesc, ProductionCode, OrderDate, DateNeeded, Week, Month, PostedFG )
SELECT * FROM @DRDetailsHist

SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryDetailsHistory] OFF 
END
END

这是否存在于存储过程查询中,因为我收到错误

  

消息208,级别16,状态1,过程SFC_spTransferDRtoDeleteArchives,第85行
  无效的对象名称'dbo.Sales.DeletedDeliveryHeaderHistory'。

我正在尝试找出一个解决方案,用于在一个存储过程中的多个表上创建多个数据传输的存储过程,只是想知道它是否可能?或者可能有解决方案......

1 个答案:

答案 0 :(得分:1)

此脚本中存在语法错误。 但是我不知道为什么你会在一个表的名称中嵌入一个句点。你需要更明智地使用方括号:

IF @TransType = 1

BEGIN

SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryHeaderHistory] ON 

-- INSERT TO DELETE DELIVERY HEADER HISTORY

INSERT INTO @DRHeaderHist (RowID, CompanyName, ItemClassCode, DeliveryDate, LoadingDate, DRNumber, Custnmbr, CustName,  TruckerID, PlateNumberID, TonnerID, DeliveryTypeID,
                            Remarks, DateCreated, DateModified, DatePosted, UserName, FGUserName, DRUserName, PostedFG, Import, CareOf, CareOfName )
SELECT * from [Sales.DeliveryHeaderHistory] WHERE DRNUMBER = @DR



----------------v
INSERT INTO dbo.[Sales.DeletedDeliveryHeaderHistory] (RowID, CompanyName, ItemClassCode,  DeliveryDate, LoadingDate, DRNumber, Custnmbr, CustName, TruckerID, PlateNumberID, TonnerID, DeliveryTypeID,
                                                  Remarks, DateCreated, DateModified, DatePosted, UserName, FGUserName, DRUserName, PostedFG, Import, CareOf, CareOfName )
SELECT * FROM @DRHeaderHist

SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryHeaderHistory] OFF 
SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryDetailsHistory] ON 

-- INSERT TO DELETE DELIVERY DETAIL HISTORY

INSERT INTO @DRDetailsHist ( RowID, CompanyName, DRNumber, OrderDocNum, OFLineNumber, LineNumber, Quantity, UofM, KgsPerBag, Itemnmbr,
                                                  TotalKgs, ItemDesc, VariantCode, VariantDesc, ProductionCode, OrderDate, DateNeeded, Week, Month1, PostedFG )
SELECT * from [dbo].[Sales.DeliveryDetailsHistory] WHERE DRNUMBER = @DR




INSERT INTO [dbo].[Sales.DeletedDeliveryDetailsHistory] ( RowID, CompanyName, DRNumber, OrderDocNum, OFLineNumber, LineNumber, Quantity, UofM, KgsPerBag, Itemnmbr,
                                                  TotalKgs, ItemDesc, VariantCode, VariantDesc, ProductionCode, OrderDate, DateNeeded, Week, Month, PostedFG )
SELECT * FROM @DRDetailsHist

SET IDENTITY_INSERT [dbo].[Sales.DeletedDeliveryDetailsHistory] OFF 

END

我注意到需要方括号。