CREATE  PROCEDURE dbo.addEntry (
  @EntryTable nvarchar(25) = NULL OUTPUT,
  @Source uniqueidentifier,
  @SourceLine int,
  @dateTime datetime = NULL,
  @cIp int = NULL,
  @csUsername nvarchar(36) = NULL,
  @sSitename varchar(12) = NULL,
  @sComputername nvarchar(15) = NULL,
  @sIp int = NULL,
  @sPort int = NULL,
  @csMethod varchar(10) = NULL,
  @csUriStem nvarchar(512) = NULL,
  @csUriQuery nvarchar(2048) = NULL,
  @scStatus smallint = NULL,
  @scWin32Status int = NULL,
  @scBytes int = NULL,
  @csBytes int = NULL,
  @timeTaken int = NULL,
  @csVersion varchar(20) = NULL,
  @csHost nvarchar(256) = NULL,
  @csUserAgent nvarchar(256) = NULL,
  @csCookie nvarchar(2048) = NULL,
  @csReferer nvarchar(512) = NULL
) AS
SET NOCOUNT ON
BEGIN TRAN

DECLARE @SQLStatement nvarchar(2000)
DECLARE @ParamDefinitions nvarchar(2000)
DECLARE @tmpCsUriStem uniqueidentifier
DECLARE @tmpCsUriQuery uniqueidentifier
DECLARE @tmpCsHost uniqueidentifier
DECLARE @tmpCsUserAgent uniqueidentifier
DECLARE @tmpCsCookie uniqueidentifier
DECLARE @tmpCsReferer uniqueidentifier

SELECT @tmpCsUriStem = [ID] FROM [URIStem]
  WHERE [cs-uri-stem] = @csUriStem
SELECT @tmpCsUriQuery = [ID] FROM [URIQuery]
  WHERE [cs-uri-query] = @csUriQuery
SELECT @tmpCsHost = [ID] FROM [Host]
  WHERE [cs-host] = @csHost
SELECT @tmpCsUserAgent = [ID] FROM [UserAgent]
  WHERE [cs(User-Agent)] = @csUserAgent
SELECT @tmpCsCookie = [ID] FROM [Cookie]
  WHERE [cs(Cookie)] = @csCookie
SELECT @tmpCsReferer = [ID] FROM [Referer]
  WHERE [cs(Referer)] = @csReferer

IF ((@csUriStem IS NOT NULL) AND (@tmpCsUriStem IS NULL))
  BEGIN
    SET @tmpCsUriStem = NEWID()
    INSERT INTO URIStem ([ID], [cs-uri-stem])
      VALUES (@tmpCsUriStem, @csUriStem)
    IF (@@ERROR <> 0) GOTO EXIT_BATCH
  END
IF ((@csUriQuery IS NOT NULL) AND (@tmpCsUriQuery IS NULL))
  BEGIN
    SET @tmpCsUriQuery = NEWID()
    INSERT INTO URIQuery ([ID], [cs-uri-query])
      VALUES (@tmpCsUriQuery, @csUriQuery)
    IF (@@ERROR <> 0) GOTO EXIT_BATCH
  END
IF ((@csHost IS NOT NULL) AND (@tmpCsHost IS NULL))
  BEGIN
    SET @tmpCsHost = NEWID()
    INSERT INTO Host ([ID], [cs-host])
      VALUES (@tmpCsHost, @csHost)
    IF (@@ERROR <> 0) GOTO EXIT_BATCH
  END
IF ((@csUserAgent IS NOT NULL) AND (@tmpCsUserAgent IS NULL))
  BEGIN
    SET @tmpCsUserAgent = NEWID()
    INSERT INTO UserAgent ([ID], [cs(User-Agent)])
      VALUES (@tmpCsUserAgent, @csUserAgent)
    IF (@@ERROR <> 0) GOTO EXIT_BATCH
  END
IF ((@csCookie IS NOT NULL) AND (@tmpCsCookie IS NULL))
  BEGIN
    SET @tmpCsCookie = NEWID()
    INSERT INTO Cookie ([ID], [cs(Cookie)])
      VALUES (@tmpCsCookie, @csCookie)
    IF (@@ERROR <> 0) GOTO EXIT_BATCH
  END
IF ((@csReferer IS NOT NULL) AND (@tmpCsReferer IS NULL))
  BEGIN
    SET @tmpCsReferer = NEWID()
    INSERT INTO Referer ([ID], [cs(Referer)])
      VALUES (@tmpCsReferer, @csReferer)
    IF (@@ERROR <> 0) GOTO EXIT_BATCH
  END

IF (@EntryTable IS NULL)
  SELECT @EntryTable = CONVERT(nvarchar(25), [MachineName] +
    N'W3SVC' + CONVERT(nvarchar(5), [SiteIndex])) FROM [LogSite]
    INNER JOIN [LogSource] ON [LogSite].[ID] = [LogSource].[Site]
    WHERE [LogSource].[ID] = @Source

IF (@EntryTable IS NULL)
  BEGIN
    RAISERROR(N'No site is defined for this source.', 16, 1)
    GOTO EXIT_BATCH
  END

SET NOCOUNT OFF

SET @SQLStatement = N'INSERT INTO ' + @EntryTable + '
  ([Source], [date-time], [c-ip], [cs-username], [s-sitename],
  [s-computername], [s-ip], [s-port], [cs-method], [cs-uri-stem],
  [cs-uri-query], [sc-status], [sc-win32-status], [sc-bytes], [cs-bytes],
  [time-taken], [cs-version], [cs-host], [cs(User-Agent)], [cs(Cookie)],
  [cs(Referer)]) VALUES (@Source, @dateTime, @cIp,
  @csUsername, @sSiteName, @sComputername, @sIp, @sPort,
  @csMethod, @tmpCsUriStem, @tmpCsUriQuery, @scStatus,
  @scWin32Status, @scBytes, @csBytes, @timeTaken,
  @csVersion, @tmpCsHost, @tmpCsUserAgent, @tmpCsCookie,
  @tmpCsReferer)'
SET @ParamDefinitions = N'@Source uniqueidentifier,
  @SourceLine int, @dateTime datetime, @cIp int,
  @csUsername nvarchar(36), @sSitename varchar(12),
  @sComputername nvarchar(15), @sIp int, @sPort int,
  @csMethod varchar(10), @tmpCsUriStem uniqueidentifier,
  @tmpCsUriQuery uniqueidentifier, @scStatus smallint,
  @scWin32Status int, @scBytes int, @csBytes int,
  @timeTaken int, @csVersion varchar(20), @tmpCsHost uniqueidentifier,
  @tmpCsUserAgent uniqueidentifier, @tmpCsCookie uniqueidentifier,
  @tmpCsReferer uniqueidentifier'

EXECUTE sp_executesql @SQLStatement, @ParamDefinitions,
  @Source, @SourceLine, @dateTime, @cIp, @csUsername,
  @sSitename, @sComputername, @sIp, @sPort, @csMethod,
  @tmpCsUriStem, @tmpCsUriQuery, @scStatus,
  @scWin32Status, @scBytes, @csBytes, @timeTaken,
  @csVersion, @tmpCsHost, @tmpCsUserAgent, @tmpCsCookie,
  @tmpCsReferer

SET NOCOUNT ON

UPDATE [LogSource] SET [LineNumber] = @SourceLine
  WHERE [ID] = @Source

EXIT_BATCH:
IF (@@ERROR <> 0)
  ROLLBACK TRAN
ELSE
  COMMIT TRAN
