Here is my trigger. LightweightDocument is my view, Document is my table.
Code:
CREATE TRIGGER InsertIntoLightweightDocument
ON dbo.LightweightDocument
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
DECLARE @CategoryID int
DECLARE @Title nvarchar(70)
DECLARE @FileName nvarchar(256)
DECLARE @FileMimeType nvarchar(100)
DECLARE @CreatorID int
DECLARE @OwnerID int
DECLARE @UpdaterID int
DECLARE @CreateTime datetime
DECLARE @UpdateTime datetime
DECLARE @Status char(1)
SELECT
@CategoryID = CategoryID,
@Title = Title,
@FileName = [Filename],
@FileMimeType = FileMimeType,
@CreateTime = CreateTime,
@UpdateTime = UpdateTime,
@Status = Status,
@UpdaterID = UpdaterID,
@OwnerID = OwnerID,
@CreatorID = CreatorID
FROM inserted
INSERT INTO dbo.Document(CategoryID, Title, [Filename], FileMimeType, CreateTime, UpdateTime, Status, UpdaterID, OwnerID, CreatorID)
VALUES(@CategoryID, @Title, @FileName, @FileMimeType, @CreateTime, @UpdateTime, @Status, @UpdaterID, @OwnerID, @CreatorID)
END
This works with the following manual SQL statement:
Code:
insert into LightweightDocument(DocumentID,CategoryID,Title,Body,[Filename],[File],FileMimeType,CreateTime,UpdateTime,Status,UpdaterID,OwnerID,CreatorID,HasAttachment)
VALUES(500,3,'t','b','a.ata',null,'application/stuff','1/1/2000','1/1/2000','A',8,8,8,0)
However, there are two problems here:
1. I am inserting with the DocumentID, the PK of the table. NHibernate doesn't send the ID to the server on insert if I am using the "identity" generator. Even though it is ignored, I still have to send it because as
xor said I have to send the same amount of arguments to the view as the number of tables returned.
2. I am not inserting the File attachment data and the body data (types "image" and "ntext"), because they aren't allowed in triggers.[/b][/code]