Author |
Topic |
sql_lover
Starting Member
14 Posts |
Posted - 2007-03-18 : 10:28:55
|
i wuld like to write a t-sql procedure (in sql 2000) which performs following operation i need a procedure that matches current date with the effective date stored in the employee table and if match found then for each employee, updates basic salary of an employee to salary after increment. pls help me regarding coz i m new to sql and sql proceduresthanx in advance. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-18 : 10:52:06
|
Sample data!Expected output!Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-18 : 12:49:30
|
You have to be more specific.Give us sample data. Give us your expected output based on the sample data.Peter LarssonHelsingborg, Sweden |
 |
|
sql_lover
Starting Member
14 Posts |
Posted - 2007-03-18 : 13:20:49
|
ok let me explain in more deepi hv two tables 1)that stores employee increment details(with amt specified as 'salary after increment') with effective date 2)that stores employye info alng with his current salarynow i want to schedule a job which runs on 1st of every month using a t-sql script in sql 2000i want this proc (script) to do the following->it matches the current date with effective date (specified in table 1)and if date matches then update the field of salary of an employee to new salary(salary after increment)now my question is that there may be more than one employee those salaries need to be updatedso i suppose this will throw some light to my problem and you'll be able to help methanx in advance |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-18 : 13:38:57
|
Give us table layouts.Give us proper sample data for the tables.Give us some idea what the table data should work after some magic intervention.Peter LarssonHelsingborg, Sweden |
 |
|
sql_lover
Starting Member
14 Posts |
Posted - 2007-03-18 : 13:59:03
|
Salary increment table Emp_id 1 2salary before increment 5200 8000 increment 500 1500salary after increment 5700 9500Effective date 1-March-2007 1-Apr-2007Employee Table Emp_Id {personal details of employee} Basic salary1 5200 2 8000 (Matches with salary before increment)now what I want is on 1st of every month it checks for each and every employee record and updates basic salary to salary after increment if effective date is current dateSo on 1st March ,emp table should look likeEmp_Id {personal details of employee} Basic salary1 5700 2 8000 So on 1stapril, emp table should look likeEmp_Id {personal details of employee} Basic salary1 5700 2 9500 |
 |
|
sql_lover
Starting Member
14 Posts |
Posted - 2007-03-19 : 03:35:43
|
this is what i hv done--Creating cursor for employee whose salary need to be updatedDECLARE empcode CURSOR FORselect empcode from trainee.incrementmaster where effectivedate=convert(varchar, getdate(), 101)--Procedure to update salaryalter procedure sal_tempasdeclare @empcode varchar(20)declare @sqlStatement varchar(20)OPEN empcodeFETCH NEXT FROM empcode INTO @empcodeWHILE @@FETCH_STATUS = 0BEGIN SET @sqlStatement = 'update trainee.employee set basic=(select salaryafterincr from trainee.incrementmaster where empcode='+ @empcode + 'where empid=' + @empcode +' '; EXEC(@sqlStatement)close empcodedeallocate empcodeend;this procedures complies with no errorbut give runtime errors as followsServer: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near 'emplo'.Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near 'emplo'.Server: Msg 16916, Level 16, State 1, Procedure sal_temp, Line 15A cursor with the name 'empcode' does not exist.Server: Msg 16916, Level 16, State 1, Procedure sal_temp, Line 16A cursor with the name 'empcode' does not exist.pls help me regarding thisthanx in advance |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-19 : 03:41:33
|
Changedeclare @sqlStatement varchar(20)todeclare @sqlStatement varchar(2000)But I am very sure of there are more clever ways to go.Peter LarssonHelsingborg, Sweden |
 |
|
sql_lover
Starting Member
14 Posts |
Posted - 2007-03-19 : 09:17:34
|
i found a even better solution there is no need to take 'sqlstatemet' variablequeries can be directly executed using following codeCREATE PROC sal_incrementASBEGIN DECLARE @empcode varchar(20) DECLARE @sqlStatement varchar(20) OPEN empcode FETCH NEXT FROM empcode INTO @empcode WHILE @@FETCH_STATUS = 0 BEGIN update trainee.employee set basic=(select salaryafterincr from trainee.incrementmaster where empcode=@empcode) where emp_id=@empcode END; CLOSE empcodeEND; |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-19 : 09:23:13
|
How fast does it run ? KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-19 : 09:27:19
|
Even better...update eset e.basic = m.salaryafterincrfrom trainee.employee AS einner join trainee.incrementmaster AS m on m.empcode = e.emp_id Peter LarssonHelsingborg, Sweden |
 |
|
|