除插入语句外,存储过程工作正常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了除插入语句外,存储过程工作正常相关的知识,希望对你有一定的参考价值。
我试图在存储过程中执行一些插入和更新,但它没有相应的工作。除插入语句外,一切正常。我无法弄清楚这个问题。
我的存储过程如下
CREATE PROCEDURE [dbo].[USP_Return_Sample_SubmitForApproval]
@Receipt_Id INT,
@Remarks NVARCHAR(2000),
@Label nvarchar(250),
@User_Remarks nvarchar(2000),
@Status char(1),
@User_Id INT,
@Role_Id INT,
@Unit_Id INT,
@Unit_Type_id INT,
@Analyst_Id INT,
@ParamXML XML
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @intErrorCode INT, @Project_Id INT, @Sample_Type NVARCHAR(100) ,@InventoryCount INT, @UpdatedCount INT
BEGIN TRAN
DECLARE @tv_Doc TABLE (ID INT IDENTITY(1,1) NOT NULL,ReceiptId INT,SampleTypeId INT,StorageId INT,RackId INT, ShelfId INT,Box_Id INT,Project_Id INT,Matrix_Type_Id INT )
INSERT INTO @tv_Doc (ReceiptId,SampleTypeId,StorageId,RackId,ShelfId,Box_Id,Project_Id,Matrix_Type_Id )
SELECT
TempDoc.Item.value('@RecieptId', 'INT'),
TempDoc.Item.value('@SampleTypeId', 'INT'),
TempDoc.Item.value('@StorageId', 'INT'),
TempDoc.Item.value('@RackId', 'INT'),
TempDoc.Item.value('@ShelfId', 'INT'),
TempDoc.Item.value('@Box_Id', 'INT'),
TempDoc.Item.value('@Project_Id', 'INT'),
TempDoc.Item.value('@Matrix_Type','INT')
FROM @ParamXML.nodes('/root/row') AS TempDoc(Item)
UPDATE [ITEMS] SET [ITEMS].Instrument_Id = [TEMP].StorageId, [ITEMS].Rack_Id = [TEMP].RackId,[ITEMS].Shelf_Id = [TEMP].ShelfId,[ITEMS].Box_Id =[TEMP].Box_Id
FROM BSMS_CPP_RCPT_RETURN_ITEMS [ITEMS] INNER JOIN BSMS_CPP_INVENTORY [INV] ON [ITEMS].Item_Barcode = [INV].Item_Barcode
INNER JOIN @tv_Doc [TEMP] ON [ITEMS].Sample_Type_Id = [TEMP].SampleTypeId AND [ITEMS].Receipt_Id = [TEMP].ReceiptId AND [INV].Box_Id = [TEMP].Box_Id
AND [TEMP].Project_Id = [ITEMS].Project_Id AND ([TEMP].Matrix_Type_Id = 0 OR [TEMP].Matrix_Type_Id = [ITEMS].Matrix_Type_Id)
UPDATE BSMS_CPP_RCPT_RETURN SET Status = @Status,Remarks =@Remarks, Created_Datetime=GETDATE() WHERE Receipt_Id = @Receipt_Id
IF (@Status = 'V')
BEGIN
UPDATE [INV] SET [INV].Storage_Type_Id=[TEMP].StorageId,[INV].Rack_Id=[TEMP].RackId,[INV].Shelf_Id=[TEMP].ShelfId
FROM BSMS_CPP_INVENTORY [INV] INNER JOIN @tv_Doc [TEMP] ON [INV].Category_Id = [TEMP].SampleTypeId AND [INV].Box_Id = [TEMP].Box_Id
AND [INV].Project_Id = [TEMP].Project_Id AND ([TEMP].Matrix_Type_Id = 0 OR [TEMP].Matrix_Type_Id = [INV].Matrix_Type_Id)
UPDATE [INV] SET [INV].Is_Exist = 1,[INV].Storage_Type_Id=[ITEMS].Instrument_Id,[INV].Rack_Id=[ITEMS].Rack_Id,[INV].Shelf_Id=[ITEMS].Shelf_Id
,Replaced_By=@User_Id,Replaced_Analyst =@Analyst_Id ,Replaced_Datetime=GETDATE()--,Category_Id = [ITEMS].Sample_Type_Id
FROM BSMS_CPP_INVENTORY [INV] INNER JOIN BSMS_CPP_RCPT_RETURN_ITEMS [ITEMS] ON [INV].Item_Barcode = [ITEMS].Item_Barcode
INNER JOIN @tv_Doc [TEMP] ON [ITEMS].Sample_Type_Id = [TEMP].SampleTypeId AND [ITEMS].Receipt_Id = [TEMP].ReceiptId AND [INV].Box_Id = [TEMP].Box_Id
AND [TEMP].Project_Id = [ITEMS].Project_Id AND ([TEMP].Matrix_Type_Id = 0 OR [TEMP].Matrix_Type_Id = [INV].Matrix_Type_Id)
WHERE [ITEMS].Receipt_Id = @Receipt_Id
DELETE FROM BSMS_CPP_INVENTORY_ANALYST WHERE BSMS_CPP_INVENTORY_ANALYST.Item_Barcode IN
(SELECT [ITEM].Item_Barcode FROM BSMS_CPP_RCPT_RETURN_ITEMS [ITEM] WHERE [ITEM].Receipt_Id = @Receipt_Id)
DECLARE @Dt DATETIME = GETDATE()
INSERT INTO BSMS_CPP_RCPT_RETURN_ESIGN
(Receipt_Id,User_Role,User_Id,User_Datetime,User_Remarks,Action_Label)VALUES
(@Receipt_Id,@Role_Id,@User_Id,@Dt,@User_Remarks,@Label)
INSERT INTO BSMS_CPP_RCPT_RETURN_ESIGN
(Receipt_Id,User_Role,User_Id,User_Datetime,User_Remarks,Action_Label)VALUES
(@Receipt_Id,@Role_Id,@Analyst_Id,@Dt,@User_Remarks,'Witnessed By')
EXEC USP_AT_INS_CPP_Return_Sample @Receipt_Id, @Dt
DECLARE @Replaced_FRC NVARCHAR(200),@Replaced_Analyst NVARCHAR(200)
SELECT @Replaced_FRC = Display_Name FROM BSMS_MST_SYS_USERS WHERE User_Id = @User_Id
SELECT @Replaced_Analyst = Display_Name FROM BSMS_MST_SYS_USERS WHERE User_Id = @Analyst_Id
UPDATE [ISSUE] SET Replaced_Samples = '1', Replaced_FRC=@Replaced_FRC,Replaced_Analyst =@Replaced_Analyst ,Replaced_Datetime=@Dt,Remarks = @Remarks
FROM BSMS_RPT_ISSUANCE [ISSUE] INNER JOIN BSMS_CPP_INVENTORY [INV] ON [ISSUE].Inventory_Id = [INV].Inventory_Id
INNER JOIN BSMS_CPP_RCPT_RETURN_ITEMS [ITEMS] ON [INV].Item_Barcode = [ITEMS].Item_Barcode
WHERE [ITEMS].Receipt_Id = @Receipt_Id AND [ISSUE].RPT_Receipt_Id = (SELECT MAX([ISS].RPT_Receipt_Id) FROM BSMS_RPT_ISSUANCE [ISS] WHERE [ISS].Inventory_Id = [INV].Inventory_Id)
END
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0)
ROLLBACK TRAN
COMMIT TRAN
Select @intErrorCode [ERROR]
END
在上面的过程中,insert语句不会将数据插入表中。
DECLARE @Dt DATETIME = GETDATE()
INSERT INTO BSMS_CPP_RCPT_RETURN_ESIGN
(Receipt_Id,User_Role,User_Id,User_Datetime,User_Remarks,Action_Label)VALUES
(@Receipt_Id,@Role_Id,@User_Id,@Dt,@User_Remarks,@Label)
INSERT INTO BSMS_CPP_RCPT_RETURN_ESIGN
(Receipt_Id,User_Role,User_Id,User_Datetime,User_Remarks,Action_Label)VALUES
(@Receipt_Id,@Role_Id,@Analyst_Id,@Dt,@User_Remarks,'Witnessed By')
请帮忙。谢谢
答案
如果您希望Id
列具有您在insert语句中提供的RecieptId
值,那么将Id
列作为Identity
列是没有意义的。
Identity列会自动为每个插入的新记录增加列计数器。
因此,要使插入工作,请从插入的值中删除@RecieptId
,并从要插入的列中删除Reciept_Id
。
或者如果你想显式插入Reciept_Id
不要像这样在create table定义中提到它
DECLARE @tv_Doc TABLE (ID INT NOT NULL,ReceiptId INT,SampleTypeId INT,StorageId INT,
RackId INT, ShelfId INT,Box_Id INT,Project_Id INT,Matrix_Type_Id INT )
以上是关于除插入语句外,存储过程工作正常的主要内容,如果未能解决你的问题,请参考以下文章