
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		marek
-- Create date: 07/04/2009
-- Description:	Dodaje wyjatek do tabeli ErrorLog, 
--				jesli istnieje juz taki wyjatek w tabeli ErrorType to uzywa go jako katogorii,
--				jesli nie, to wstawia nowa kategorie wyjatku
-- =============================================
ALTER PROCEDURE [dbo].[sp_LogException]
	-- Add the parameters for the stored procedure here
	@errorMessage nvarchar(100),
	@userName nvarchar(50),
	@dateTime datetime,
	@errorDetails nvarchar(4000)
AS
BEGIN

	DECLARE @errorTypeId INT

	SELECT @errorTypeId=Id FROM dbo.ErrorType WHERE ErrorMessage=@errorMessage

	IF (@errorTypeId IS NULL OR @errorTypeId<=0)
		BEGIN
			INSERT INTO dbo.ErrorType(ErrorMessage) VALUES (@errorMessage)
			SELECT @errorTypeId=SCOPE_IDENTITY()
		END

	INSERT INTO dbo.ErrorLog(ErrorTypeId, UserName, [DateTime], ErrorDetails) 
	VALUES (@errorTypeId, @userName, @dateTime, @errorDetails)

END
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

