Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

t-sql required - select statement stored in a table

$
0
0

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

ruleidruledef
1select empid, case when dob is null then duedate=dob-10 else duedate=getdate()
2select empid, case when dob is null then duedate=hiredate-10 else duedate=getdate()
3select empid, case when dob is null then duedate=dob-8 else duedate=getdate()
4select empid, case when dob is null then duedate=hiredate-8 else duedate=getdate()
5select empid, case when dob is null  and dept=1 then duedate=dob-10  else duedate=getdate()
6select empid, case when dob is null then duedate=dob-5 else duedate=getdate()
7select empid, case when dob is null then duedate=dob-10 else duedate=getdate()+10
8select empid, case when dob is null then duedate=jod-10 else duedate=getdate()
9select empid, case when dob is null then duedate=leavedate-10 else duedate=getdate()

 '#emp' table

empidruleidsaleidhiredatejoddobduedate
11102-jan-201303-jan-201313-nov-1976 
12202-jan-201303-jan-201313-nov-1976 
13402-jan-201303-jan-201313-nov-1976 
17602-jan-201303-jan-201313-nov-1976 
19302-jan-201303-jan-201313-nov-1976 
22102-jan-201303-jan-201308-jun-1980 
24202-jan-201303-jan-201308-jun-1980 
27402-jan-201303-jan-201308-jun-1980 
22602-jan-201303-jan-201308-jun-1980 
21502-jan-201303-jan-201308-jun-1980 
33102-jan-201303-jan-20131-jan-2000 
38202-jan-201303-jan-20131-jan-2000 
37302-jan-201303-jan-20131-jan-2000 
36402-jan-201303-jan-20131-jan-2000 
32502-jan-201303-jan-20131-jan-2000 
31602-jan-201303-jan-20131-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')










Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>