I want to generate 'duedate' in '#emp' table based on rules (ruleid) that are stored as sql statements in '#rule' table. I put only 3 empid's. In real time, I will have more than that. I need a stored procedure. This stored procedure will be run in both ways. There will be an empid will be sent from application to stored procedure and also I have to run this stored procedure as a part of daily job. The both will generate 'duedate'. I need help on this, please.
'#rule' table
ruleid | ruledef |
1 | select empid, case when dob is null then duedate=dob-10 else duedate=getdate() |
2 | select empid, case when dob is null then duedate=hiredate-10 else duedate=getdate() |
3 | select empid, case when dob is null then duedate=dob-8 else duedate=getdate() |
4 | select empid, case when dob is null then duedate=hiredate-8 else duedate=getdate() |
5 | select empid, case when dob is null and dept=1 then duedate=dob-10 else duedate=getdate() |
6 | select empid, case when dob is null then duedate=dob-5 else duedate=getdate() |
7 | select empid, case when dob is null then duedate=dob-10 else duedate=getdate()+10 |
8 | select empid, case when dob is null then duedate=jod-10 else duedate=getdate() |
9 | select empid, case when dob is null then duedate=leavedate-10 else duedate=getdate() |
'#emp' table
empid | ruleid | saleid | hiredate | jod | dob | duedate |
1 | 1 | 1 | 02-jan-2013 | 03-jan-2013 | 13-nov-1976 | |
1 | 2 | 2 | 02-jan-2013 | 03-jan-2013 | 13-nov-1976 | |
1 | 3 | 4 | 02-jan-2013 | 03-jan-2013 | 13-nov-1976 | |
1 | 7 | 6 | 02-jan-2013 | 03-jan-2013 | 13-nov-1976 | |
1 | 9 | 3 | 02-jan-2013 | 03-jan-2013 | 13-nov-1976 | |
2 | 2 | 1 | 02-jan-2013 | 03-jan-2013 | 08-jun-1980 | |
2 | 4 | 2 | 02-jan-2013 | 03-jan-2013 | 08-jun-1980 | |
2 | 7 | 4 | 02-jan-2013 | 03-jan-2013 | 08-jun-1980 | |
2 | 2 | 6 | 02-jan-2013 | 03-jan-2013 | 08-jun-1980 | |
2 | 1 | 5 | 02-jan-2013 | 03-jan-2013 | 08-jun-1980 | |
3 | 3 | 1 | 02-jan-2013 | 03-jan-2013 | 1-jan-2000 | |
3 | 8 | 2 | 02-jan-2013 | 03-jan-2013 | 1-jan-2000 | |
3 | 7 | 3 | 02-jan-2013 | 03-jan-2013 | 1-jan-2000 | |
3 | 6 | 4 | 02-jan-2013 | 03-jan-2013 | 1-jan-2000 | |
3 | 2 | 5 | 02-jan-2013 | 03-jan-2013 | 1-jan-2000 | |
3 | 1 | 6 | 02-jan-2013 | 03-jan-2013 | 1-jan-2000 |
create table #role
(ruleid int, ruledef varchar(500))
insert into #role
select (1,'select empid, case when dob is null then duedate=dob-10 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (2,'select empid, case when dob is null then duedate=hiredate-10 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (3,'select empid, case when dob is null then duedate=dob-8 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (4,'select empid, case when dob is null then duedate=hiredate-8 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (5,'select empid, case when dob is null and dept=1 then duedate=dob-10 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (6,'select empid, case when dob is null then duedate=dob-5 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (7,'select empid, case when dob is null then duedate=dob-10 else duedate=getdate()+10 end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (8,'select empid, case when dob is null then duedate=jod-10 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
union
select (9,'select empid, case when dob is null then duedate=leavedate-10 else duedate=getdate() end as duedate from #emp e inner join #rule r on r.ruleid=e.ruleid ')
create table #emp
(empid int,ruleid int,saleid int,hiredate datetime,jod datetime,dob datetime,duedate datetime)
insert into #emp
select (1,1,1,'02-jan-2013','03-jan-2013','13-nov-1976')
union
select (1,2,2,'02-jan-2013','03-jan-2013','13-nov-1976')
union
select (1,3,4,'02-jan-2013','03-jan-2013','13-nov-1976')
union
select (1,7,6,'02-jan-2013','03-jan-2013','13-nov-1976')
union
select (1,9,3,'02-jan-2013','03-jan-2013','13-nov-1976')
union
select (2,2,1,'02-jan-2013','03-jan-2013','08-jun-1980')
union
select (2,4,2,'02-jan-2013','03-jan-2013','08-jun-1980')
union
select (2,2,6,'02-jan-2013','03-jan-2013','08-jun-1980')
union
select (2,2,1,'02-jan-2013','03-jan-2013','08-jun-1980')
union
select (2,1,5,'02-jan-2013','03-jan-2013','08-jun-1980')
union
select (3,3,1,'02-jan-2013','03-jan-2013','1-jan-2000')
union
select (3,8,2,'02-jan-2013','03-jan-2013','1-jan-2000')
union
select (3,7,3,'02-jan-2013','03-jan-2013','1-jan-2000')
union
select (3,6,4,'02-jan-2013','03-jan-2013','1-jan-2000')
union
select (3,2,5,'02-jan-2013','03-jan-2013','1-jan-2000')
union
select (3,1,6,'02-jan-2013','03-jan-2013','1-jan-2000')