Skip to content
Advertisement

SQL Trigger throws error but still inserts

I am working on a trigger that is supposed to block an insert when @verkoperstatus is 0; this does function, but for some reason it also stops the insert when @verkoperstatus is 1. What could be the root cause behind this?

It should insert when @verkoperstatus is 1, and raise an error when @verkopstatus is 0.

The table Gebruiker is references, which includes a ‘gebruikersnaam’ and a ‘verkoper’ column. The value of the ‘gebruikersnaam’ column is the identifying column which (in this specific case is ‘Lars’). Verkoper is a bit column, which indicated if one is a seller or not, so this has the value of a 0 or a 1.

The goal I am trying to achieve is to have an insert on the Verkoper tabel if a ‘gebruikersnaam’ has the ‘verkoper’ value of one. This means if there is a row in Gebruiker with the ‘gebruikersnaam’ of Lars and the verkoper has a value of 1. This will be an allowed insert into the Verkoper tabel.

As the Verkoper has the following columns: ‘gebruikersnaam’, ‘banknaam’, ‘rekeningnummer’, ‘controleoptienaam’ and ‘creditcardnummer’. When ‘gebruikersnaam’ corresponds with a ‘gebruikersnaam’ from the Gebruikers table AND has a value of 1 in the ‘verkoper’ column this record will be allowed to be inserted into the Verkoper table.

As of now there is a row in the Gebruikers column which includes the gebruikersnaam ‘Lars’ and a verkoper value of ‘1’. Meaning any SQL Insert with the gebruikersnaam of ‘Lars’ should be allowed into the Verkoper table.

This however does not function the way I believe it should.

These are the tables mentioned above:

For the inserts I am using the following data:

Advertisement

Answer

This is untested, however, I suspect this is the logic you really need:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement