A sp should do some tasks:update or insert data in Telling,decrement data from another table TellingR.ALTER PROCEDURE [dbo].[spMutanrH] -- Add the parameters for the stored procedure here ( @ScanNummer NVARCHAR(13), @Basis tinyint, @CurrentTal INT OUT, @CurrentTalH INT OUT)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;DECLARE @Return TABLE ( Value INT NOT NULL, ValueH int not null );-- Increment the Telling Tal in Telling MERGE dbo.Telling AS tgtUSING ( SELECT TOP(1) ProductID, 1 AS Tal, GETDATE() AS [Date],@Basis as Basis FROM dbo.Product WHERE ScanNummer = @ScanNummer ) AS src ON src.ProductID = tgt.ProductIDWHEN MATCHED THEN UPDATE SET tgt.Tal += src.Tal ,tgt.Basis = @BasisWHEN NOT MATCHED BY TARGET THEN INSERT ( ProductID, Tal, [Date], Basis ) values ( src.ProductID, src.Tal, src.[Date], src.Basis )OUTPUT inserted.TalINTO @Return ( ValueH );-- Decrement the Telling Tal in TellingREET MERGE dbo.TellingR AS tgtUSING ( SELECT TOP(1) ProductID, 1 AS Tal, GETDATE() AS [Date],@Basis as Basis FROM dbo.Product WHERE ScanNummer = @ScanNummer ) AS src ON src.ProductID = tgt.ProductIDWHEN MATCHED THEN UPDATE SET tgt.Tal -= src.Tal ,tgt.Basis = @BasisOUTPUT inserted.TalINTO @Return ( Value );SELECT @CurrentTal = ISNULL(Value, CAST(@Scannummer AS INT)),@CurrentTalH = ISNULL(Value, CAST(@Scannummer AS INT))FROM @Return;END
I try to figure it out what's wrongSSMS messages:Msg 515, Level 16, State 2, Procedure spMutatienrH, Line 27Cannot insert the value NULL into column 'Value', table '@Return'; column does not allow nulls. INSERT fails.The statement has been terminated.Msg 515, Level 16, State 2, Procedure spMutatienrH, Line 54Cannot insert the value NULL into column 'ValueH', table '@Return'; column does not allow nulls. INSERT fails.The statement has been terminated.(1 row(s) affected)(1 row(s) affected)if you do not try, it will not work