0
從未寫過觸發器。SQL Server INSERT/UPDATE觸發器連接日期字符串
需要插入和更新時檢查日期的觸發器,如果日期大於25日,則將該日期設置回該月的25日。
現在我正在嘗試構建@newDate,我不知道該怎麼做。
目前代碼:
CREATE TRIGGER [dbo].[setASDate]
ON [dbo].[AutoShip]
AFTER UPDATE, INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @DistID INT;
DECLARE @date INT;
DECLARE @month INT;
DECLARE @year INT;
IF UPDATE(DateNextShipment)
BEGIN
DECLARE @newDate datetime;
SELECT TOP 1 @DistID = DistID FROM INSERTED
SELECT TOP 1 @date = DATEPART(d, DateNextShipment) FROM INSERTED
SELECT TOP 1 @month = CONVERT(varchar(2),DATEPART(mm, DateNextShipment)) FROM INSERTED
SELECT TOP 1 @year = CONVERT(varchar(4),DATEPART(yyyy, DateNextShipment)) FROM INSERTED
@newDate = CONVERT(datetime,@year + '-' + @month + '-25')
If @date > 25
BEGIN
UPDATE AutoShip
SET DateNextShipment = @newDate, Day = 25
WHERE DistID = @DistID
END
END
END
編輯 - 更新了代碼
看起來不錯,但我'm仍然會出現'@newDate'錯誤附近的語法錯誤 感謝您的幫助 – Silverwulf
其實,現在這種方式就像一種魅力,我需要改變一件小事情。你呢。 – Silverwulf