简体   繁体   中英

SQL Server trigger to conditionally update a column in the same table I am inserting or updating to

New to posting in this forum by have been lurking for years. I have a trigger question that seems to be above my head in both innate knowledge and researching skills.

I have created an appointment scheduler app pointed to a SQL Server 2008 database that works similarly to the way Outlook works but have come to one impass that needs to be solved. I need to create a trigger to update a column on one or more rows of the appointments table representing the number of appointments that exist at the same date and time when another appointment is added, updated or deleted.

So if I add, update or delete an appointment, the trigger would look to see how many appointments exist at the time and date of the inserted/changed/deleted record and update the records that exist with the same time and date to reflect the new number of appointments remaining at that time slot. Does that make sense? Any ideas?

Thanks a ton for your help!

Why store AppointmentsInSlot when you can simply query to find how many and not have to worry about triggers:

SELECT
    [ScheduleTime],
    [AppointmentsInSlot] = COUNT(ID)
FROM [dbo].[Appointments]
GROUP BY [ScheduleTime]

If needed add any where clause to filter to the days you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM