I have a table (VPOFF) that has a row for each 15 minute increment an office is open. In another table (VPVAC) are rows that define the start and stop time for employee vacations. In one procedure I'm inserting some bulk vacation rows, and I need to increment the SFULL column by 1 in the VPOFF table for each 15 minute time increment covered by the vacation rows inserted. My first instinct was to write something roughly like:
I understand why that doesn't work. The update doesn't know how to match up the VACSTART and VACSTOP fields since each subquery returns multiple values. I know that Uncle Rico would suggest using a cursor to loop through each of the vacation rows I just inserted and update VPOFF individually, but my RICOMETER tells me that's not a good idea. So, I took a bathroom break, because naturally all problems can be solved in the bathroom and wrote this when I got back to my desk:
The last select statement shows all the correct rows modified, but the code doesn't run real fast (possibly something to do with using temp tables?). So, is there a better way to achieve this? #a is is a mock-up of my VPOFF table and @b is a mock-up of my VPVAC table. Like I said, the data returned at the end is correct in my example, but if I'm doing this the long way I'd like to know.
I need to wash myself clean of the Rico practices....
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
Code:
[COLOR=blue]update[/color]
VPOFF
[COLOR=blue]set[/color]
SFULL = SFULL + 1
[COLOR=blue]where[/color]
VACDATE >= ([COLOR=blue]select[/color] VACSTART [COLOR=blue]from[/color] VPVAC [COLOR=blue]where[/color] [COLOR=blue]timestamp[/color] = [i](the [COLOR=blue]timestamp[/color] [COLOR=blue]from[/color] the [COLOR=blue]bulk[/color] [COLOR=blue]insert[/color])[/i])
and VACDATE < ([COLOR=blue]select[/color] VACSTOP [COLOR=blue]from[/color] VPVAC [COLOR=blue]where[/color] [COLOR=blue]timestamp[/color] = [i](the [COLOR=blue]timestamp[/color] [COLOR=blue]from[/color] the [COLOR=blue]bulk[/color] [COLOR=blue]insert[/color])[/i])
I understand why that doesn't work. The update doesn't know how to match up the VACSTART and VACSTOP fields since each subquery returns multiple values. I know that Uncle Rico would suggest using a cursor to loop through each of the vacation rows I just inserted and update VPOFF individually, but my RICOMETER tells me that's not a good idea. So, I took a bathroom break, because naturally all problems can be solved in the bathroom and wrote this when I got back to my desk:
Code:
[COLOR=blue]set[/color] [COLOR=#FF00FF]nocount[/color] [COLOR=blue]on[/color]
[COLOR=blue]create[/color] [COLOR=blue]table[/color] #a (vacdate [COLOR=blue]smalldatetime[/color], sfull [COLOR=blue]tinyint[/color])
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 09:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 10:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 11:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 12:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 08:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 09:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 10:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 11:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-02 12:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 08:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 09:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 10:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 11:00'[/color], 0
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] #a [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 12:00'[/color], 0
[COLOR=blue]declare[/color] @b [COLOR=blue]table[/color] (vacstart [COLOR=blue]smalldatetime[/color], vacstop [COLOR=blue]smalldatetime[/color])
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]select[/color] [COLOR=red]'2007-01-01 08:00'[/color], [COLOR=red]'2007-01-01 12:00'[/color]
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]select[/color] [COLOR=red]'2007-01-03 10:00'[/color], [COLOR=red]'2007-01-03 11:00'[/color]
[COLOR=blue]set[/color] [COLOR=#FF00FF]nocount[/color] [COLOR=blue]off[/color]
[COLOR=blue]update[/color]
#a
[COLOR=blue]set[/color]
sfull = sfull + 1
[COLOR=blue]where[/color]
#a.vacdate in (
[COLOR=blue]select[/color]
a.vacdate
[COLOR=blue]from[/color]
#a [COLOR=blue]as[/color] a
[COLOR=blue]inner[/color] [COLOR=blue]join[/color]
@b [COLOR=blue]as[/color] b
[COLOR=blue]on[/color]
a.vacdate >= b.vacstart
and a.vacdate < b.vacstop
)
[COLOR=blue]select[/color] * [COLOR=blue]from[/color] #a
[COLOR=blue]drop[/color] [COLOR=blue]table[/color] #a
The last select statement shows all the correct rows modified, but the code doesn't run real fast (possibly something to do with using temp tables?). So, is there a better way to achieve this? #a is is a mock-up of my VPOFF table and @b is a mock-up of my VPVAC table. Like I said, the data returned at the end is correct in my example, but if I'm doing this the long way I'd like to know.
I need to wash myself clean of the Rico practices....
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson