Link to home
Start Free TrialLog in
Avatar of Robert Burns
Robert Burns

asked on

Another Trigger head scratcher

Been working on this for a couple days.

Right now my trigger looks like this:
CREATE TRIGGER FM_Insert_Location
ON spemp
FOR INSERT, UPDATE
AS
INSERT FM_Location
select i.email_address, i.spbldinf_code, i.spfloors_code, i.spbldrom_code,
'N', GetDate()
from inserted i join deleted d on i.spemp_code = d.spemp_code
where i.spbldrom_code <> d.spbldrom_code

Everything works fine, but I would like it to recognize when the email_address field has a null or blank value and NOT post the record in those cases.

I feel like I've tried everything :)  Thoughts?
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are talking about the new email_address than a WHERE clause is all you need.

CREATE TRIGGER FM_Insert_Location
ON spemp
FOR INSERT, UPDATE
AS
INSERT FM_Location
select i.email_address, i.spbldinf_code, i.spfloors_code, i.spbldrom_code,
'N', GetDate()
from inserted i join deleted d on i.spemp_code = d.spemp_code
where i.spbldrom_code <> d.spbldrom_code
          And IsNull(email_address, '') <> ''

CREATE TRIGGER FM_Insert_Location
ON spemp
FOR INSERT, UPDATE
AS
INSERT FM_Location
-- (good practice to put the list of column names here, makes maintenance MUCH simpler)
select i.email_address, i.spbldinf_code, i.spfloors_code, i.spbldrom_code,
'N', GetDate()
from inserted i
left outer join deleted d
    on i.spemp_code = d.spemp_code
where i.spbldrom_code <> d.spbldrom_code
 and  i.email_address is not null



Avatar of Robert Burns
Robert Burns

ASKER

Oi vey!  the number of ways I tried this and it was so simple.
But I have to admit, the ending syntax of this line leaves me wondering:

and IsNull(i.email_address, '') <> ''

The first quote is inside the parenthesis and the other isn't?
It is not double quote. These are 2 single quotes.

IsNull(i.email_address, '') returns '' when the email_address is null otherwise it returns actual email_address. Now we compare it with empty string and that is the condition.
Ahhh, I see.  Thanks a bunch for the explanation.