简体   繁体   English

在SQL Server 2000中触发

[英]trigger in sql server 2000

I have a trigger that supposed to update log out time[generate random log out time within an hour of log in time, seconds have to differ too], how do I achieve that functionality, please help here is my trigger so far: 我有一个触发器,该触发器应该更新注销时间[在登录时间的一小时内生成随机注销时间,秒数也必须有所不同],我如何实现该功能,请到目前为止帮助我的触发器:

USE TestDB
GO
CREATE TRIGGER AfterInsertTG
ON dbo.usage_reports_Dummy2
AFTER INSERT AS

    DECLARE @pk_id as int, @member_id as int,@login_time AS DATETIME,@logout_time AS DATETIME
    ,@ip AS VARCHAR(255),@session_id AS VARCHAR(255);
    SELECT 
        @pk_id = pk_id ,
        @member_id = member_id,
        @login_time =login_time,
        @logout_time = logout_time,
        @ip = ip,
        @session_id = session_id
    FROM
        usage_reports_Dummy2


    IF(@logout_time IS NULL)

        BEGIN
        ??????? 
        END     

GO

Thank you all for helping me out specially Eric for taking some time and think about the formula, I chose Marc's answer becuase it suits my conditions 谢谢大家特别帮助我,埃里克(Eric)花了一些时间思考一下公式,我选择了马克的答案,因为它适合我的条件

here is the final code: 这是最终代码:

CREATE TRIGGER trgInsert
ON dbo.usage_reports_Dummy2
INSTEAD OF INSERT
AS BEGIN
    INSERT INTO 
       dbo.usage_reports_Dummy2(member_id, login_time, logout_time, ip, session_id)
          SELECT 
             member_id, login_time, logout_time, ip, session_id
          FROM inserted 
          WHERE logout_time IS NOT NULL

    INSERT INTO 
       dbo.usage_reports_Dummy2(member_id, login_time, logout_time, ip, session_id)
          SELECT 
              member_id, login_time, DATEADD(ss, RAND() * 3600, login_time), 
              ip, session_id
          FROM inserted 
          WHERE logout_time IS NULL
END

You can use newid to generate a random sequence for you, then send it to checksum to get an int out of it, and then mod ( % ) it by 3600 (seconds in an hour). 您可以使用newid为您生成一个随机序列,然后将其发送到checksum以获取int值,然后对其进行mod% 3600(一小时的秒数)。 Using the dateadd function, you can add that random amount of seconds to your @login_time . 使用dateadd函数,您可以将随机的秒数添加到@login_time

select @logout_time = dateadd(ss, checksum(newid()) % 3600, @login_time)

It should be noted that rand only returns one value per statement, so all of your rows would have the same variance between @login_time and @logout_time . 应当注意, rand每条语句仅返回一个值,因此您的所有行在@login_time@logout_time之间将具有相同的方差。

However, if is a single row, then this works well: 但是,如果是单行,则效果很好:

select @logout_time = dateadd(ss, rand() * 3600, @login_time)

First thing you have to understand in writng a trigger is that you cannot assume that only one record will be processed at a time. 在编写触发器时,首先要了解的是,您不能假设一次只能处理一条记录。 SO first you need to rethink your process to not use variables that way. 因此,首先,您需要重新考虑您的过程,以免以这种方式使用变量。 Trigger must be written to account for set-based operations. 必须将触发器写入帐户才能进行基于集合的操作。

Triggers have two pseuddotables intserted and deleted which store the information you need. 触发器有两个插入和删除的伪表,它们存储您所需的信息。 In your case inserted has what you want. 在您的情况下插入您想要的。 something like this (using the idea from above) might work: 这样的事情(使用上面的想法)可能会起作用:

update u
set logout_time = dateadd(ss, checksum(newid()) % 3600, login_time)
from usage_reports_Dummy2 u
join inserted i on u.id = i.id
where logout_time is null

This will update anyrecords that were inthe current insert that do not have a logout_time to the random value based onthe formula dateadd('ss', checksum(newid()) % 3600, login_time). 这将根据公式dateadd('ss,checksum(newid())%3600,login_time)将当前插入内容中没有logout_time的所有记录更新为随机值。

Test this and see what it will do. 测试一下,看看它会做什么。 Possibly the formula will need tweaking. 公式可能需要调整。

What you could do is create an INSTEAD OF INSERT trigger something like this: 您可以做的是创建一个INSTEAD OF INSERT触发器,如下所示:

CREATE TRIGGER trgInsert
ON dbo.usage_reports_Dummy2
INSTEAD OF INSERT
AS BEGIN
    INSERT INTO 
       dbo.usage_reports_Dummy2(member_id, login_time, logout_time, ip, session_id)
          SELECT 
             member_id, login_time, logout_time, ip, session_id
          FROM inserted 
          WHERE logout_time IS NOT NULL

    INSERT INTO 
       dbo.usage_reports_Dummy2(member_id, login_time, logout_time, ip, session_id)
          SELECT 
              member_id, login_time, DATEADD(ss, RAND() * 3600, login_time), 
              ip, session_id
          FROM inserted 
          WHERE logout_time IS NULL
END

That way, if a "logout_time" is provided, the values are stored "as is", and if not, then your logout_time is calculated based on your requirements. 这样,如果提供了“ logout_time”,则值将“按原样”存储,否则,将根据您的要求计算logout_time。

Marc

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

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