我有一個觸發器應該更新註銷時間[在註冊時間內產生一個小時內的隨機註銷時間,秒也必須不同],我該如何實現該功能,請在這裏幫助是我的觸發至今:觸發器在SQL Server 2000中
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
謝謝大家幫助我特意埃裏克抽出一些時間和思考的公式,我選擇了馬克的回答,原因是其符合我的條件
這裏是最後的代碼:
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
感謝您的幫助,但它給我無效參數錯誤DATEADD指定 無效的參數1。 – Developer 2009-07-23 20:30:10
它應該是DATEADD(ss,rand()* 3600,@login_time) - 周圍沒有引號 – 2009-07-23 21:04:36
已修復。對於那個很抱歉。 – Eric 2009-07-23 21:05:51