尝试学习触发器,似乎无法为这是否是实现此目标的最佳方法而wrap之以鼻。我想算一共“票”,并将其放入“订单”列中。
CREATE TRIGGER DAILYTICKETTOTALS
ON db.ORDER
AFTER UPDATE
AS
BEGIN
INSERT INTO db.ORDER (
--Finding the order they made for the day:
SELECT * FROM db.ORDER
WHERE DESCRIPTION = 'specific description' AND
CAST(CAST(CAST(GETDATE()AS float) AS int) AS datetime) =
CAST(CAST(CAST(db.ORDER.STARTDATE AS float) AS int) AS datetime)
AND db.ORDER.CREATEDBY = 'Last, First'
)
SET db.ORDER.COLUMNTOTALS VALUES(
--Finding the total of tickets input for the day
SELECT COUNT(*)
FROM db.ticket
WHERE CAST(CAST(CAST(db.ticket.DATE AS float) AS int) AS datetime) =
CAST(CAST(CAST(GETDATE()AS float) AS int) AS datetime)
AND db.ticket.INPUTBY = 'Last, First');
感谢您的帮助,感谢您对此进行审查。
答案 0 :(得分:0)
对触发器中可用的“特殊”表进行一些研究,例如“ INSERTED”,因为这将为您提供已更新行的详细信息,并且您可以将其与其他任何内容(包括实际表)联系起来正在更新)。请记住,您不应该假设仅更新了1行,因为触发器将每批触发一次,而不是每行触发一次。
这是存储过程的一个版本,该版本假定每个订单都有一个名为OrderID的唯一ID,并且票证和订单表都具有personID,而不是按名称查找它们。如果没有,他们应该这样做,如果您不能更改它,则使用现有的混乱连接。
CREATE TRIGGER DAILYTICKETTOTALS
ON db.ORDER
AFTER UPDATE
AS
BEGIN
update o
set columntotals=TicketNum
from inserted
join db.[order] o on o.orderid=inserted.orderid
join (
select convert(date,ticket,[date]) as ticketdate, PersonID, count(*) as TicketNum
from db.ticket
group by convert(date,ticket,[date]) as ticketdate, PersonID
) t on t.PersonID=o.PersonID and t.ticketdate=convert(date,getdate())
答案 1 :(得分:-1)
这可以解决我的问题,以防万一您好奇。我不知道它是否理想,但是可以。
CREATE TRIGGER DAILYTICKETTOTALS
ON db.ORDER
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @INS NVARCHAR(50)
SELECT @INS = NAMECOLUMN FROM INSERTED
WHERE DESCRIPTION = 'specific description' AND
CAST(CAST(CAST(STARTDATE AS float) AS int) AS datetime)=
CAST(CAST(CAST(GETDATE() AS float) AS int) AS datetime)
END
IF @INS = ‘Last, First’
BEGIN
UPDATE db.ORDER
SET COLUMNTOTALS = EXAMPLENAME.CT
FROM (SELECT COUNT(*) AS CT FROM ticket WHERE INPUTBY = ‘Last, First’) EXAMPLENAME
WHERE DESCRIPTION = 'specific description' AND INPUTBY = ‘Last, First’
END
BEGIN