USE [TVM] GO /****** Object: UserDefinedFunction [dbo].[getClearedEventFaultsLogPK] Script Date: 09/28/2009 12:18:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: RAN02 -- Create date: 2009-09-27 -- Description: Returns the PK from FaultsLog for an event that was cleared -- ============================================= ALTER FUNCTION [dbo].[getClearedEventFaultsLogPK] ( @iEventPK int, @iDevicePK int ) RETURNS int AS BEGIN -- Declare the return variable here DECLARE @iFKFaultsLog int -- Add the T-SQL statements to compute the return value here SET @iFKFaultsLog = ( SELECT TOP 1 FL.PK FROM TVM.dbo.FaultsLog FL JOIN TVM.dbo.EventsLog EL ON FL.fkEventsLog = EL.PK WHERE EL.fkEventDefinitions = @iEventPK and EL.SetFlag = 1 and FL.fkDevices = @iDevicePK ORDER BY EL.Created DESC ) -- Return the result of the function RETURN @iFKFaultsLog END