Tuesday, April 26, 2011

Renumbering Table Rows

For the life of me, I can never remember the syntax for this!  Ack!

-- hack to get WITH expression to work
set @user_code = @user_code;

/* renumber now that we have the data massaged how we want */
WITH newPH
AS (SELECT
  [rowid]
  ,ROW_NUMBER() OVER(ORDER BY AUDIT_LOG_DATA_ID DESC) AS [newRowID]
FROM   web_sp_AUDIT_LOG_View
WHERE  userkey = @userkey)
--SELECT * FROM newPH
UPDATE newPH
SET    [rowid] = newRowID

declare @total_records int
SELECT @total_records = COUNT(*) FROM web_sp_AUDIT_LOG_View WHERE userkey = @userkey
UPDATE web_sp_AUDIT_LOG_View SET [total_records] = @total_records where userkey = @userkey

No comments:

Post a Comment