CREATE TRIGGER [dbo].[OnUpdateContractFile]
ON [dbo].[ContractFile]
FOR UPDATE AS
BEGIN
-- =============================================
-- Author:		Bokor J. Márk
-- Adapted:		dr. Dózsa András
-- Create date: 2014.08.06
-- Description:	General insert trigger
--              to assign avatarId
-- =============================================
SET NOCOUNT ON
--work wariables
DECLARE @ContractFilesId Int
DECLARE @NewAvatarID Int
--Table Type Detection
DECLARE @TriggeredTableId Int
--DataSpace fill variables
DECLARE @ContractFilesName nvarchar(50)
DECLARE @DataSpaceName nvarchar(256)
DECLARE @DataSpaceDescription nvarchar(80)

--query our trigger's id
SELECT @TriggeredTableId = Id FROM TableType WHERE Name = 'ContractFile'

DECLARE tmpCursor CURSOR LOCAL FOR
SELECT Inserted.Id, Inserted.AvatarId, Inserted.Name
FROM Inserted INNER JOIN Deleted ON Inserted.Id = Deleted.Id
OPEN tmpCursor
FETCH NEXT FROM tmpCursor INTO @ContractFilesId, @NewAvatarID, @ContractFilesName
WHILE (@@FETCH_STATUS = 0)
BEGIN
--Insert DataSpace Entry
SET @DataSpaceName = CAST(@TriggeredTableId as varchar) + '.' + CAST(@ContractFilesId as varchar)
SET @DataSpaceDescription =  'Szerződés állomány: ' + @ContractFilesName
EXEC sp_SaveToDataSpace @NewAvatarID, @DataSpaceName, @DataSpaceDescription, 2
-- get next record
FETCH NEXT FROM tmpCursor INTO @ContractFilesId, @NewAvatarID, @ContractFilesName
END
CLOSE tmpCursor
DEALLOCATE tmpCursor
END