1

Normally I like solving things on my own, but i started SQLServer not too long ago and it's giving me a headache.

Anyways, the question was to Write a trigger that will Place 'No First Name' in any record added to the customerEmployee table that has a NULL for the EmpFirstName field.

What I have been done was:

  `CREATE TRIGGER noFirstName `
  `ON dbo.CUSTOMEREMPLOYEE`
  `FOR INSERT, UPDATE AS`
   <code>print 'No First Name'</code>

Then I wrote another query to test it out but it returns me an error that says:

Cannot insert the value NULL into column 'TITLE', table 'Chapter8.dbo.CUSTOMEREMPLOYEE'; column does not allow nulls. INSERT fails. 
The statement has been terminated.

Can anyone see what is wrong?

Cezar
  • 55,636
  • 19
  • 86
  • 87
UltSonicEx
  • 13
  • 3
  • Yes the trigger should be changed as Adel indicated. But I'm pretty sure your error doesn't relate to the trigger itself. FOR defaults to an AFTER trigger and you have a constraint on the TITLE column that won't let you insert NULLS.. AFTER triggers execute after the data has been inserted and passed constraint checks so in order to do what you need you'll need to set the TITLE column to nullable. You could use an INSTEAD OF trigger to change what's being inserted which should be pre-constraint – Shane Neuville Jul 14 '13 at 06:18
  • i see what i did wrong! Thanks Guys! – UltSonicEx Jul 14 '13 at 20:13

1 Answers1

1

I think the problem is that you are printing : print 'No First Name' but not inserting the value into the table!

Refer to this question: Insert Update trigger

Community
  • 1
  • 1
Adel Khayata
  • 2,717
  • 10
  • 28
  • 46