Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Proc Fails

Author  Topic 

JohnDW
Starting Member

45 Posts

Posted - 2014-10-18 : 13:43:45
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
)

AS
BEGIN
-- 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 tgt
USING (
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.ProductID
WHEN MATCHED
THEN UPDATE
SET tgt.Tal += src.Tal ,tgt.Basis = @Basis
WHEN NOT MATCHED BY TARGET
THEN INSERT (
ProductID,
Tal,
[Date], Basis
) values
(
src.ProductID,
src.Tal,
src.[Date], src.Basis
)OUTPUT inserted.Tal
INTO @Return
(
ValueH
);
-- Decrement the Telling Tal in TellingREET
MERGE dbo.TellingR AS tgt
USING (
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.ProductID
WHEN MATCHED
THEN UPDATE
SET tgt.Tal -= src.Tal ,tgt.Basis = @Basis


OUTPUT inserted.Tal
INTO @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 wrong
SSMS messages:
Msg 515, Level 16, State 2, Procedure spMutatienrH, Line 27
Cannot 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 54
Cannot 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

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-18 : 17:45:16
OK, so column inserted.Tal is null for some row. Your table definition does not allow nulls. What do you want to do when inserted.Tal is Null?
Go to Top of Page
   

- Advertisement -