'SQL Server 2017 Triggers dont respect database design and null value conditions inside it fail

I have two key problems with triggers in SQL Server 2017. They are:

  1. They don't respect database design.
  2. NULL value conditions in the trigger fail.

Let me explain:

1. They don't respect database design: Lets say I have tables and a view like below and I am trying to create a trigger which would insert into multiple tables based on what has been inserted into the view. (taken and edited from this post - Instead of trigger to update view with multiple tables)


CREATE TABLE persons
(personid  int, 
 firstname varchar(32) default 'Charles', 
 lastname  varchar(32));

CREATE TABLE employees
(employeeid int, 
 personid   int, 
 title      varchar(32));

CREATE VIEW vwEmployees AS
SELECT p.personid, employeeid, firstname, lastname, title
  FROM employees e JOIN persons p
    ON e.personid = p.personid;

CREATE TRIGGER tgEmployeesInsert ON vwEmployees
INSTEAD OF INSERT AS
BEGIN
  INSERT INTO persons (personid, firstname, lastname)
  SELECT personid, firstname, lastname
    FROM INSERTED

  INSERT INTO employees (employeeid, personid, title)
  SELECT employeeid, personid, title
    FROM INSERTED
END;

INSERT INTO vwEmployees (personid, employeeid, lastname, title)
VALUES(1, 1, 'Doe', 'SQL Developer');

If I use the select statement like below:

select * from persons

This will fetch me results as below:

personid firstname lastname
1 Doe

I am not sure if you noticed above but the default for firstname should have been 'Charles' as mentioned in the create table above.

2. NULL value conditions in the trigger fail.

Here, I used the same method as above for creating an instead of trigger for updating a view with multiple tables. Lets say I have a trigger like below:

CREATE or ALTER TRIGGER oc.trg_Update_Tracker ON oc.BP_Tracker
INSTEAD OF Update AS
BEGIN  
       Begin Try
       SET NOCOUNT ON
       Begin
            if update(Partner_ID) or update(Partner_Name) or update(LEI) 
            Begin
                Update oc.Business_Partner
                SET Partner_ID=ins.Partner_ID, Partner_Name=ins.Partner_Name, LEI=ins.LEI, User_Changed = ins.User_Changed
                from oc.Business_Partner bp, inserted as ins, deleted as del
                where bp.Partner_ID = del.Partner_ID and bp.Partner_Name = del.Partner_Name
            End
.........

Lets say the bp.Partner_Name is null and del.Partner_Name is null. In this case, the update statement fails because the condition bp.Partner_Name = del.Partner_Name fails in the trigger. If the same update statement were to be passed outside the trigger on the table - that works. Also, if I remove the condition bp.Partner_Name = del.Partner_Name, the trigger update would work.

Would really appreciate if someone could explain me what's going on.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source