Somebody's probably going to shoot me for suggesting it but (utilizing Steve's wonderful DDL and sample data), perhaps something like this:
CREATE TABLE #eventLog
(
sessionID INT NOT NULL
,logID TIMESTAMP NOT NULL
,logText VARCHAR(1000) NULL
,created DATETIME NULL
,PRIMARY KEY CLUSTERED (sessionID, logID)
,UNIQUE (sessionID)
)
;WITH cteSampleData (sessionID,logText,created)
AS
(
SELECT 1001,'George Washington','2013-03-31' UNION ALL
SELECT 1002,'John Adams','2013-02-28' UNION ALL
SELECT 1003,'Thomas Jefferson','2013-02-15' UNION ALL
SELECT 1004,'James Madison','2013-02-01' UNION ALL
SELECT 1005,'James Monroe','2013-01-31' UNION ALL
SELECT 1006,'John Q Adams','2013-01-22' UNION ALL
SELECT 1007,'Andrew Jackson','2013-01-13'
)
INSERT INTO #eventLog
(sessionID,logText,created)
SELECT
sessionID
,logText
,created
FROM
cteSampleData
SELECT
*
FROM
#eventLog;
WITH LastSessionID AS (
SELECT LastSessionID=MAX(sessionID) FROM #eventLog WITH(UPDLOCK))
INSERT INTO #eventLog (sessionID,logText,created)
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) + LastSessionID
,logText,created
FROM (
-- Your newest inserts go here
SELECT 'Babe Ruth', '2013-01-20'
UNION ALL SELECT 'Ty Cobb', '2013-01-21'
) a (logText,created)
CROSS APPLY LastSessionID b
SELECT *
FROM #eventLog
GO
DROP TABLE #eventLog
Table name changed to a temp table to protect the innocent.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St