April 27, 2011 at 3:42 pm
Hello I am trying to run on trigger but it is not working please help me. Thank you
UPDATE Emp
SET First_name = CASE WHEN (i.P_Name IS NULL) THEN i.f_Name
ELSE i.P_Name end
FROM inserted i
JOIN ADP_Emp ON i.id = Emp.id
April 27, 2011 at 4:16 pm
Why do you open yet another thread?
Aren't two enough? (http://www.sqlservercentral.com/Forums/FindPost1098960.aspx and http://www.sqlservercentral.com/Forums/FindPost1098990.aspx)
April 27, 2011 at 4:31 pm
because this is another issues
I been working for hours and can't do it, can you please help me
Thank you
April 27, 2011 at 4:47 pm
Please explain what the issue is and show all the code. You must tell us what you think should happen and what is not happening. From the code, there is nothing wrong with it.
April 27, 2011 at 5:15 pm
I have 2 columns and need create a trigger
Put trigger on nickname on insert and updAte
If nickname is not null then
Firstname=nickname
If firstname is insert or update and nickname is null
Update firstname =firstname
I put 2 statements together
But only 1st working
Case statement is not working
Thank you
April 27, 2011 at 9:17 pm
You have the logic there, just work through it.
First, check nickname. It it is null, then what? It looks like you are going a little too quickly with the logic. The set firstname = firstname makes no sense.
Go through your various scenarios. You can have
- Firstname and nickname null
- nickname with a value and firstname null
- firstname with a value and nickname null
- both firstname and nickname with values.
In each case, what is the result you need? If it's just set one value to the other if it is null, then that's easy:
update mytable
set nickname = firstname
from inserted i
where mytable.id = i.id
and nickname is null
The other case is the reverse.
update mytable
set firstname = nickname
from inserted i
where mytable.id = i.id
and firstname is null
Or you might add an "is not null" to each query like this:
set firstname = nickname
from inserted i
where mytable.id = i.id
and firstname is null
and nickname is not null
Always code your update/insert as a select first in a trigger. That way if multiple rows are changed/inserted/deleted, you can handle it.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply