stored procedures - Using loop to insert multiple row in SQL Server 2008? -


i have temp table store data calculating payroll. data related many tables , long query logic.

this stored procedure data other tables insert payrolltemp table later use. needs 3 parameters: start , end date, , employeeid (empid) main query , nested queries.

so whenever run stored procedure,

(1 row(s) affected)

my question how use loop in sql server pass every employeeid (empid) sequentially store procedure in order insert every record payrolltemp table.

create procedure [dbo].[sp_payrolltemp_init] @startdate date, @enddate date, @empid int   insert dbo.payrolltemp([empid],[basicsalary],[allowance],[w_hrs],[w_days],[ot150hrs],[ot200hrs],[ot300hrs],[night130hrs], [nightot150hrs],[nightot200hrs],[nightot300hrs],[weekdaydutydays],[weekenddutydays],[otherearning],[si],[hi],[ui],[unionfee],[dependentdeduction],[otherdeduction])         select e.id,e.basicsalary,e.allowance,         (select sum(ts.normalhours) timesheeta ts empid=@empid)w_hrs,         (select            (datediff(dd, @startdate, @enddate) + 1)           -(datediff(wk, @startdate, @enddate) * 2)           -(case when datename(dw, @startdate) = 'sunday' 1 else 0 end)           -(case when datename(dw, @enddate) = 'saturday' 1 else 0 end))w_days,         (select sum(ts.ot150hrs) timesheeta ts empid=@empid , timesheetdate between @startdate , @enddate)ot150hrs,         (select sum(ts.ot200hrs) timesheeta ts empid=@empid , timesheetdate between @startdate , @enddate)ot200hrs,         (select sum(ts.ot300hrs) timesheeta ts empid=@empid , timesheetdate between @startdate , @enddate)ot300hrs,         (select sum(ts.night130hrs) timesheeta ts empid=@empid , timesheetdate between @startdate , @enddate)night130hrs,         (select sum(ts.nightot150hrs) timesheeta ts empid=@empid , timesheetdate between @startdate , @enddate)nightot150hrs,         (select sum(ts.nightot200hrs) timesheeta ts empid=@empid , timesheetdate between @startdate , @enddate)nightot200hrs,         (select sum(ts.nightot300hrs) timesheeta ts empid=@empid , timesheetdate between @startdate , @enddate)nightot300hrs,          (select count(empid)weekdayduty nightdutyschedule datename(dw,dutydate) not in('saturday','sunday') , dutydate between @startdate , @enddate , empid=@empid)weekdaydutydays,         (select count(empid) nightdutyschedule datename(dw,dutydate)='saturday' or datename(dw,dutydate)='sunday' , dutydate between @startdate , @enddate , empid=@empid)weekenddutydays,         (select sum(earningamount) earningdetails earningdate between @startdate , @enddate , empid=@empid)otherearning,         (select case e.is_probation             when  'false'             e.basicsalary*0.08             else 0 end            )si,           (select case e.is_probation             when  'false'             e.basicsalary*0.015             else 0 end            )hi,           (select case e.is_probation             when  'false'             e.basicsalary*0.01             else 0 end            )ui,           (select case e.is_probation             when  'false'             case  when e.basicsalary > 4000000 or e.basicsalary = 4000000  20000   else 15000 end             else 0 end            )unionfee,           (select case e.is_probation             when  'false'                 e.dependants * 3600000             else 0 end            )dependentdeduction,          (select sum(deductionamount) deductiondetails deductiondate between @startdate , @enddate , empid=@empid)otherdeduction           employee e e.is_active='true' , e.foreigner='false' , e.id=@empid     declare @startdate date, @enddate date, @empid int     set @startdate='2015-09-01'     set @enddate='2015-09-30'     set  @empid='3'     select e.id,e.basicsalary,e.allowance,sum(ts.normalhours)w_hrs,     (select                (datediff(dd, @startdate, @enddate) + 1)               -(datediff(wk, @startdate, @enddate) * 2)               -(case when datename(dw, @startdate) = 'sunday' 1 else 0 end)               -(case when datename(dw, @enddate) = 'saturday' 1 else 0 end))w_days,         sum(ts.ot150hrs)ot150hrs,sum(ts.ot200hrs)ot200hrs, sum(ts.ot300hrs)ot300hrs,sum(ts.night130hrs) night130hrs,         sum(ts.nightot150hrs)nightot150hrs,sum(ts.nightot200hrs)nightot200hrs,sum(ts.nightot300hrs)nightot300hrs,         (select count(case                  when datename(dw,sch.dutydate) not in('saturday','sunday')                     1                 else 0 end                )weekdaydutydays,          (select count(case                  when datename(dw,sch.dutydate)  in('saturday','sunday')                     1                 else 0 end                )weekenddutydays,         sum(ed.earningamount)otherearning,         (select case e.is_probation                 when  'false'                 e.basicsalary*0.08                 else 0 end                )si,               (select case e.is_probation                 when  'false'                 e.basicsalary*0.015                 else 0 end                )hi,               (select case e.is_probation                 when  'false'                 e.basicsalary*0.01                 else 0 end                )ui,               (select case e.is_probation                 when  'false'                 case  when e.basicsalary > 4000000 or e.basicsalary = 4000000  20000   else 15000 end                 else 0 end                )unionfee,               (select case e.is_probation                 when  'false'                     e.dependants * 3600000                 else 0 end                )dependentdeduction,              sum(ded.deductionamount) otherdeduction      employee e      inner join timesheeta ts     on e.id=ts.empid , e.is_active='true' , e.foreigner='false'      inner join nightdutyschedule sch     on e.id=sch.empid , sch.dutydate >= @startdate , sch.dutydate <= @enddate     inner join  earningdetails ed     on e.id=ed.empid , ed.earningdate >= @startdate , ed.earningdate <= @enddate     inner join deductiondetails ded     on e.id= ded.empid , ded.deductiondate >= @startdate , ded.deductiondate <= @enddate 

i modifed use inner join statement when excute , display error this:

    msg 102, level 15, state 1, line 62 incorrect syntax near '@enddate'. 

line 62 is: on e.id= ded.empid , ded.deductiondate >= @startdate , ded.deductiondate <= @enddate (it last one, why?)

you have use cursor: msdn - cursor

some example code:

declare cursorname cursor fast_forward select a,b table > 1 declare @a int declare @b varchar(10) open cursorname fetch next cursorname @a,     @b  while ( @@fetch_status = 0 )  begin --code , procedure want call @a , @b each row fetch next cursorname @a, @b end close cursorname deallocate cursorname 

hope helps :)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -