October 12, 2006 at 12:43 pm
Can anyone please let me know if they have written code for a scenario that i have.
Theres a table A which has columns as
EmployeID
Employee Name
ManagerId,
ManagerName
We have data as follows
1111 aaa 1000 zzz
2222 bbb 1000 zzz
3333 ccc 1000 zzz
Now in this case when doing an insert we need to make sure that for an employee the manager is already an existing data else it will show up as blank. So when inserting data into teh table the selection should be in the order
2000 www (doesnt have a mgr)
1000 zzz 2000 www
1111 aaa 1000 zzz
2222 bbb 1000 zzz
3333 ccc 1000 zzz
How this could be done within a sql statement. Any help on this will be greatly appreciated.
TIA
October 12, 2006 at 3:47 pm
If I understand this correctly, when an insert occurs, you want to:
1. Check for the existince of the mananager;
2. If the manager is not already listed as an employee, then add them (but without a manager for themselves).
Is this correct?
If so; then:
create trigger AA on A for insert
as
insert into A (EmployeID, EmployeName)
select i.ManagerId, i.ManagerName
from inserted i
where not exists (select 1 from A where i.ManagerId = A.EmployeID)
go
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply