Author |
Topic |
christopher_kennard
Starting Member
4 Posts |
Posted - 2008-04-09 : 15:37:53
|
Here is a simplified example of a problem I am facing. I have 2 tables: Tasks and Employees.Tasks:(Task_ID, Task_Name, Task_Type, Task_Requirement, Employee_ID)Employees:Emp_ID, Emp_Name, Emp_Specialty, Emp_Task_Cnt, Max_Task_CntRequirements: Write a MS SQLServer 2000 Storeed Procedure to:1. Update the Tasks table by assigning the task to an Employee.2. Incrememnt the employee's Emp_Task_Cnt for each Task assigned.3. Match the Employee to the Task by matching the Task_Requirement to the Emp_Specialty.4. Do not exceed the employee's Max_Task_Cnt.I have a working solution to the requirements, but it involves using cursor logic. For all the obvious reasons, I wanted to avoid using a cursor (or cursor-like looping structure) but could not figure out any other way to avoid processing the Task table one record at a time because of the: "4. Do not allow an Employee's Task_Cnt to exeed the Max_Task_Cnt."Q: Is there a way to do this without using a cursor and still meet all of the requirements?BELOW IS MY SOLUTION USING A CURSOR-- create proc to test requirementscreate proc dbo.proc_assign_tasks asdeclare @task_id int, @task_req char(3)select task_id, task_requirementinto #tasksfrom taskwhere emp_id is nulldeclare tsk cursor forselect task_id, task_requirementfrom #tasksopen tskfetch next from tsk into @task_id, @task_reqdeclare @emp_id int, @result varchar(3)while @@fetch_status = 0beginset @emp_id = null ; set @result = nullselect top 1 @emp_id = emp_idfrom employeewhere emp_specialty = @task_reqand emp_task_cnt < emp_max_task_cntorder by emp_task_cntif @emp_id is not nullbeginupdate taskset emp_id = @emp_idwhere task_id = @task_idupdate employeeset emp_task_cnt = emp_task_cnt + 1where emp_id = @emp_idand emp_specialty = @task_reqset @result = 'Yes'endelse set @result = 'No'insert assign_task_log (task_id,emp_id, result) values(@task_id, @emp_id, @result)fetch next from tsk into @task_id, @task_reqendclose tskdeallocate tskgo-- test tables and load test datacreate table dbo.task (task_id tinyint, task_requirement char(3), emp_id tinyint null)goinsert task (task_id,task_requirement) values( 1, 'AAA')insert task (task_id,task_requirement) values( 2, 'AAA')insert task (task_id,task_requirement) values( 3, 'AAA')insert task (task_id,task_requirement) values( 4, 'BBB')insert task (task_id,task_requirement) values( 5, 'BBB')insert task (task_id,task_requirement) values( 6, 'BBB')insert task (task_id,task_requirement) values( 7, 'CCC')insert task (task_id,task_requirement) values( 8, 'CCC')insert task (task_id,task_requirement) values( 9, 'CCC')insert task (task_id,task_requirement) values( 10, 'CCC')insert task (task_id,task_requirement) values( 11, 'CCC')gocreate table dbo.employee (emp_id tinyint, emp_specialty char(3), emp_task_cnt tinyint, emp_max_task_cnt tinyint)goinsert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (1,'AAA',9,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (2,'AAA',10,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (3,'AAA',10,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (4,'AAA',8,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (5,'AAA',8,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (6,'AAA',9,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (7,'BBB',9,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (8,'BBB',8,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (9,'BBB',10,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (10,'CCC',10,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (11,'CCC',7,10)insert dbo.employee (emp_id, emp_specialty, emp_task_cnt, emp_max_task_cnt) values (12,'CCC',8,10)gocreate table dbo.assign_task_log (task_id tinyint, emp_id tinyint, result varchar(3))go-- test proc w/ test dataselect * from taskselect * from employeegoexec dbo.proc_assign_tasks goselect * from taskselect * from employeeselect * from assign_task_loggo |
|
bbasir
Yak Posting Veteran
76 Posts |
Posted - 2008-04-09 : 18:26:34
|
your requirements are not very clear... hwoever here is my take on this.you can update the count using the following logic.1. should not require a cusrosr this is just a simple update or insert....you can just use basic insert into table ... select values statment to get this doen...2. the incrementing of counter could be done this way...insert into tableselect DECLARE @counter intSET @counter = 0UPDATE Emp_Task_Cnt SET @counter = counter = @counter + 13, is a basic join between the tables..4, you can have a where clause in in the insert into selct stament that where the count is less than some value... |
 |
|
christopher_kennard
Starting Member
4 Posts |
Posted - 2008-04-09 : 23:43:13
|
bbasir, I am not seeing how your suggestion could work. look at waht the code in the proc does. what the proc does with a cursor, I wolud like get some help (understandable help) the can allow me to use a set based approach instead.Can you flesh out what you are saying a bit more?"requirements unclear"? I could not have simplified the problem to a more base level. what is unclear to you.1. assign the tasks to employees. 2. keep track of the running total of the total assignments given to each employee3. employees cannot be assigned more tasks than thier max task limit.forget the requirements. Analaze the Proc. It's very straight forward. |
 |
|
|
|
|