Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2004-06-25 : 04:12:49
|
| I have 2 tablesThe 1st table emp_details contains all the details and there are emps with pass and failed.Active is a field which determines which students have been taken for special assessmentemp_detailsemp_id Dept_id Passed Active2356 Sc Y 04567 Sc N 06457 sc Y 05688 Sr Y 04234 Sc N 06793 Sc N 05721 MA N 01279 EN Y 0Now I need a query which says select 5 passed(Y) emp_ids and 5 failed emp_ids(Passed =N)may be top 5 of each category and then make the field active by updating the field Active to 1 which shows that the ACtive=1 students are selected.Now the second part.As soon as the update is done with Active = 1 a trigger should be there where those emp_ids and the passed field data should be inserted to a new table called Active_emps as shown below.Active_empsemp_id Passed 2356 Y 4567 N 6457 Y 5688 Y 4234 N 6793 N 5721 N 1279 YThis process is a continous process where the next top 10 or 20 emp_ids will be selected which are not active = 0 and then updated to 1 and then that corressponding datas are inserted.Please help with this queries |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-06-25 : 06:15:22
|
| No point in a triggerdeclare @t table (emp_id int)insert @t select top 5 emp_id from emp_details where Passed = 'Y' and Active = 0insert @t select top 5 emp_id from emp_details where Passed = 'N' and Active = 0begin traninsert Active_empsselect e.emp_id, e.passedfrom @t tjoin emp_details eon e.emp_id = t.emp_idif @@error <> 0beginraiserror('failed', 16, -1)rollback tranreturnendupdate emp_detailsset Active = 1from emp_details ejoin @t ton e.emp_id = t.emp_idif @@error <> 0beginraiserror('failed', 16, -1)rollback tranreturnendcommit tran==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|