I have two tables like this:
Table1
Emp_ID Emp_Name Age Level
1 Tom 22
2 Susan 50
Table2
Level Min_Age Max_Age
Associate 20 30
Senior 31 45
What I want to do is to find which level each employee is at. As such, I want to:
Do for all records in Table1 (All employees) i. Select Level from Table2 where Table1.Age is between Min_Age and Max_Age in Table2 ii.Move to next employee
I have tried the following:
DECLARE @level int, DECLARE @employee_age int
DELCARE @min_age varcahr int, @max_age varchar int
DECLARE testcursor CURSOR
LOCAL SCROLL STATIC
FOR
Select Emp_ID FROM Table1
OPEN testcursor
FETCH NEXT FROM testcursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM testcursor
@min_age=select Min_age from Table2
@max_age= select Max_age from Table2
@employee_age=Select age FROM Table1
@level=select Level from Table2 where
update Table1 set level=@level where @employee_age between @min_age and max_age
END
CLOSE cursorName
DEALLOCATE cursorName
This doesn't completely work since I have to cursor through the records in Table2 as well. Any help would be appreciated.