以下是SQL Server 2014 Management Studio生成的脚本:
BEGIN TRANSACTION
CREATE PARTITION FUNCTION [fc_DispatchingHistory](smalldatetime) AS RANGE LEFT FOR VALUES (N'2015-12-31T00:00:00')
CREATE PARTITION SCHEME [scm_DispatchingHistory] AS PARTITION [fc_DispatchingHistory] TO ([Earlier_than_2016_FileGroup], [PRIMARY])
ALTER TABLE [dbo].[tbl_Dispatching_Audit] DROP CONSTRAINT [FKEY_tbl_Dispatching_Audit_Dispatching_ID_tbl_Dispatching_Dispatching_ID]
ALTER TABLE [dbo].[tbl_Dispatching] DROP CONSTRAINT [PK_tbl_Dispatching]
ALTER TABLE [dbo].[tbl_Dispatching] ADD CONSTRAINT [PK_tbl_Dispatching] PRIMARY KEY NONCLUSTERED
(
[Dispatching_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
CREATE CLUSTERED INDEX [ClusteredIndex_on_scm_DispatchingHistory_636131462265663820] ON [dbo].[tbl_Dispatching]
(
[Pickup_Date]
)WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [scm_DispatchingHistory]([Pickup_Date])
DROP INDEX [ClusteredIndex_on_scm_DispatchingHistory_636131462265663820] ON [dbo].[tbl_Dispatching]
ALTER TABLE [dbo].[tbl_Dispatching_Audit] WITH CHECK ADD CONSTRAINT [FKEY_tbl_Dispatching_Audit_Dispatching_ID_tbl_Dispatching_Dispatching_ID] FOREIGN KEY([Dispatching_ID])
REFERENCES [dbo].[tbl_Dispatching] ([Dispatching_ID])
ALTER TABLE [dbo].[tbl_Dispatching_Audit] CHECK CONSTRAINT [FKEY_tbl_Dispatching_Audit_Dispatching_ID_tbl_Dispatching_Dispatching_ID]
COMMIT TRANSACTION
执行时始终显示以下错误:
Msg 402,Level 16,State 1,Procedure tbl_Dispatching,Line 4
数据类型smalldatetime和time在add运算符中不兼容。
该表的触发器如下:
ALTER TRIGGER [dbo].[Delay_Timestamp] ON [dbo].[tbl_Dispatching]
FOR UPDATE
AS
-- Update Actual_Pickup_Time if "Status_ID" has changed to 5
UPDATE disp
SET Actual_Pickup_Time = SYSDATETIME()
FROM dbo.tbl_Dispatching disp
INNER JOIN Inserted i ON i.Dispatching_ID = disp.Dispatching_ID -- use PRIMARY KEY of table!
INNER JOIN Deleted d ON d.Dispatching_ID = disp.Dispatching_ID -- use PRIMARY KEY of table!
WHERE
i.Status_ID = 5 -- new value is 5
AND d.Status_ID <> 5 -- old value was NOT 5
-- Update Actual_Delivery_Time if "Status_ID" has changed to 8
UPDATE disp
SET Actual_Delivery_Time = SYSDATETIME()
FROM dbo.tbl_Dispatching disp
INNER JOIN Inserted i ON i.Dispatching_ID = disp.Dispatching_ID -- use PRIMARY KEY of table!
INNER JOIN Deleted d ON d.Dispatching_ID = disp.Dispatching_ID -- use PRIMARY KEY of table!
WHERE
i.Status_ID = 8 -- new value is 8
AND d.Status_ID <> 8 -- old value was NOT 8
触发器将所有插入和更新操作捕获到另一个表中:
ALTER TRIGGER [dbo].[FillDispatching_For_Edit_Log]
ON [dbo].[tbl_Dispatching]
AFTER UPDATE
AS
INSERT INTO tbl_Dispatching_Log SELECT top 1 'Update' AS Actione_Mod, GETDATE() Action_DateTime, * FROM inserted
MS SQL Server是: Microsoft SQL Server 2014(SP2)(KB3171021) - 12.0.5000.0(X64) 2016年6月17日19:14:09 版权所有(c)Microsoft Corporation Windows NT 6.3(Build 9600:)(管理程序)
上的企业版(64位)