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 |
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2008-05-23 : 06:26:10
|
| Hello,I'm really struggling with some syntax and Id like some help please if you have the time.I have an employee table with several fields including employee number and holiday group in it.I also have a table with several rows in it relating to a holiday. So for example, it would have holiday date, employee number etc.I then have a stored procedure which does a load of stuff, once you pass it in a holiday group and a date.I'd like to write a trigger on the employee table, so that when a person's holiday group is changed, it will query the holidays table to get all dates within there pass them into the stored procedure.Obviously as the stored procedure only takes a holiday group and 1 date, I'd need this to loop.Does this make sense ?If so, I'd appreciate any help you can offer.If not, please let me know so I can try and explain more.Thanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-23 : 07:36:45
|
quote: Originally posted by Jonny1409 Hello,I'm really struggling with some syntax and Id like some help please if you have the time.I have an employee table with several fields including employee number and holiday group in it.I also have a table with several rows in it relating to a holiday. So for example, it would have holiday date, employee number etc.I then have a stored procedure which does a load of stuff, once you pass it in a holiday group and a date.I'd like to write a trigger on the employee table, so that when a person's holiday group is changed, it will query the holidays table to get all dates within there pass them into the stored procedure.Obviously as the stored procedure only takes a holiday group and 1 date, I'd need this to loop.Does this make sense ?If so, I'd appreciate any help you can offer.If not, please let me know so I can try and explain more.Thanks.
What will your stored procedure be doing? |
 |
|
|
nduggan23
Starting Member
42 Posts |
Posted - 2008-05-23 : 07:56:28
|
as long as your stored procedure doesnt also update the employee table you could create a cursor within the trigger to loop throught the results from the holidays table then call the sp.something like this... DECLARE @hg VARCHAR(100), @date datetimeDECLARE r1_cursor CURSOR FORWARD_ONLY FOR SELECT h.holidayGroup, h.[date] FROM holidays h INNER JOIN Inserted e on e.empID = h.empID OPEN r1_cursor FETCH NEXT FROM r1_cursor INTO @hg, @date WHILE @@FETCH_STATUS = 0 BEGIN Execute procedure @hg, @date FETCH NEXT FROM r1_cursor INTO @hg, @date END CLOSE r1_cursor DEALLOCATE r1_cursor Not sure how jood an cursor is within a trigger but thats a different story... |
 |
|
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2008-05-23 : 07:59:12
|
| Basically, it moves holiday entries from one table to another depending on space (the holidays have daily limits against them)In terms of the trigger though, it doesn't really affect it - once the trigger can pass in a date and a holiday group, the sp will so the rest. |
 |
|
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2008-05-23 : 08:01:29
|
| Hi nduggan23, No the sp will not be updating the Employee Table. |
 |
|
|
Jonny1409
Posting Yak Master
133 Posts |
Posted - 2008-05-23 : 08:21:05
|
| Hi nduggan23 - I'm not sure what you've done will work, as HolidayGroup is only held in the Employee Table, not the Holiday Table. |
 |
|
|
nduggan23
Starting Member
42 Posts |
Posted - 2008-05-23 : 12:18:21
|
| Add another join to link the employee table to the empID and you get your holiday group. |
 |
|
|
|
|
|
|
|