简体   繁体   English

SQL Server触发器有条件地更新我要插入或更新到的同一张表中的列

[英]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. 我创建了一个约会调度程序应用程序,该应用程序指向一个SQL Server 2008数据库,该数据库的工作方式与Outlook的工作方式类似,但是遇到了一个需要解决的难题。 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: 当您只需查询以查找多少而不用担心触发器时,为什么要存储AppointmentsInSlot:

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

If needed add any where clause to filter to the days you want. 如果需要,请添加任何where子句以过滤所需的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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