I've created an INSTEAD OF INSERT trigger on a child table that will automatically create a record in the parent table if necessary. I have the trigger working, but I am afraid it will be a maintenance headache moving forward as I had to explicitly list each non-autonumber field in the child table.
The following is working SQL which should demonstrate what I am trying to accomplish:
CREATE TABLE Accts
(AcctNum char(3) NOT NULL CONSTRAINT PK_Accts PRIMARY KEY
,StatusCode char(1))
CREATE TABLE Docs
(DocID int NOT NULL IDENTITY (1,1) CONSTRAINT PK_Docs PRIMARY KEY
,AcctNum char(3) NOT NULL CONSTRAINT FK_Doc_AcctNum FOREIGN KEY REFERENCES Accts(AcctNum)
,SavedBy varchar(30) NOT NULL
,SavedAt datetime NOT NULL)
GO
CREATE TRIGGER Tr_I_Docs ON Docs
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO Accts (AcctNum, StatusCode)
SELECT DISTINCT i.AcctNum, 'N' FROM inserted as i
WHERE NOT EXISTS
(SELECT 1 FROM Accts AS A
WHERE A.AcctNum=i.AcctNum);
INSERT INTO Docs (AcctNum, SavedBy, SavedAt)
SELECT i.AcctNum, i.SavedBy, i.SavedAt FROM inserted as i;
END
GO
INSERT INTO Docs(AcctNum,SavedAt,SavedBy)
VALUES
('111','2014-03-12','Jim')
,('222','2014-03-13','Joe')
,('333','2014-03-14','Tom')
,('111','2014-03-21','Dan')
SELECT * FROM Accts;
GO
DROP TABLE Docs;
DROP TABLE Accts;
As columns are changed in the Docs
table I will need to remember to update this trigger. Ideally, I would like to replace these lines from the trigger...
INSERT INTO Docs (AcctNum, SavedBy, SavedAt)
SELECT i.AcctNum, i.SavedBy, i.SavedAt FROM inserted as i;
...with something more like this:
INSERT INTO Docs SELECT *
Obviously, the above won't work due to the IDENTITY column in the Docs
table. Any suggestions?
I'd also welcome ideas besides using an INSTEAD OF INSERT trigger to accomplish my goals.