CREATE TRIGGER [dbo].[OnUpdateLeasingFile]
ON [dbo].[LeasingFile]
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 @LeasingFilesId Int
DECLARE @NewAvatarID Int
--Table Type Detection
DECLARE @TriggeredTableId Int
--DataSpace fill variables
DECLARE @LeasingkFilesName nvarchar(50)
DECLARE @DataSpaceName nvarchar(256)
DECLARE @DataSpaceDescription nvarchar(80)

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

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 @LeasingFilesId, @NewAvatarID, @LeasingkFilesName
WHILE (@@FETCH_STATUS = 0)
BEGIN
--Insert DataSpace Entry
SET @DataSpaceName = CAST(@TriggeredTableId as varchar) + '.' + CAST(@LeasingFilesId as varchar)
SET @DataSpaceDescription =  'Bérlet ügylet állomány:' + @LeasingkFilesName
EXEC sp_SaveToDataSpace @NewAvatarID, @DataSpaceName, @DataSpaceDescription, 2
-- get next record
FETCH NEXT FROM tmpCursor INTO @LeasingFilesId, @NewAvatarID, @LeasingkFilesName
END
CLOSE tmpCursor
DEALLOCATE tmpCursor
END