
/****** Object:  StoredProcedure [addSession] ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[addSession]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
    DROP PROCEDURE [addSession];
GO
	
CREATE PROCEDURE [dbo].[addSession]

(
	@UserID nvarchar(100),
	@DTSDtart datetime,
	@DTSEnd datetime=null,
	@DTSActivity datetime,
	@MachineName nvarchar(100),
	@ProcessID int,
	@newSessionID int output,
	@newLastChanged timestamp output
)
WITH EXECUTE AS OWNER
AS
BEGIN TRY -- Try Block
	BEGIN TRANSACTION
	INSERT INTO [Sessions]
	(
		[UserID],
		[DTSDtart],
		[DTSEnd],
		[DTSActivity],
		[MachineName],
		[ProcessID]
	)
	VALUES
	(
		@UserID,
		@DTSDtart,
		@DTSEnd,
		@DTSActivity,
		@MachineName,
		@ProcessID
	)
	SELECT @newSessionID= SCOPE_IDENTITY()
	SELECT 		@newLastChanged=[LastChanged]
		FROM [Sessions] WHERE [SessionID]=@newSessionID
	IF( @@TRANCOUNT > 0 ) COMMIT
END TRY
BEGIN CATCH -- Catch Block
	IF( @@TRANCOUNT = 1 ) ROLLBACK -- Only rollback if top level
 	ELSE IF( @@TRANCOUNT > 1 ) COMMIT -- Otherwise commit.  Top level will rollback
	EXEC vlnErrorHandler
END CATCH
GO
-- Display the status of Proc creation
IF (@@Error = 0) PRINT 'Procedure Creation: addSession Succeeded'
ELSE PRINT 'Procedure Creation: addSession Error on Creation'
GO
