What happens if another user adds a duplicate row to expenzsection between your
IF EXISTS
and your
insert into Expenzsection
??
I know that situation would be very rare
but if it happens it will be completely unreproducible and very hard to debug.
You could look at isolation levels, or
INSERT INTO Expenzsection(...
SELECT @sectionname,...
WHERE NOT EXISTS
(
SELECT * FROM expenzsection WHERE ...
)
SELECT @Result = CASE WHEN @@ROWCOUNT = 0 THEN -1 ELSE @@ERROR END