Ok, I have a table without a natural key, only an integer identifier column as the primary key. I would like to insert and get the identifier value, but also use a trigger to ensure that certain fields are always set. Initially, the design was to be used instead of insert triggers, but this violates scope_identity. The output clause in the insert statement is also interrupted instead of the insert trigger. So, I came up with an alternative plan and would like to know if there is something clearly wrong with what I intend to do:
start a far-fetched example:
CREATE TABLE [dbo].[TestData] (
[TestId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] [nchar](10) NOT NULL)
CREATE TABLE [dbo].[TestDataModInfo](
[TestId] [int] PRIMARY KEY NOT NULL,
[RowCreateDate] [datetime] NOT NULL)
ALTER TABLE [dbo].[TestDataModInfo] WITH CHECK ADD CONSTRAINT
[FK_TestDataModInfo_TestData] FOREIGN KEY([TestId])
REFERENCES [dbo].[TestData] ([TestId]) ON DELETE CASCADE
CREATE TRIGGER [dbo].[TestData$AfterInsert]
ON [dbo].[TestData]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[TestDataModInfo]
([TestId],
[RowCreateDate])
SELECT
[TestId],
current_timestamp
FROM inserted
END
Complete a contrived example.
No, I do not do this for one small date field - this is just an example.
, , , ( TestDataModInfo), . , scope_identity() ( , ). , , ?