HeadId AreaId BranchId SiteId Description
11 12 16 8 Metal - Brass
11 12 16 8 Metal - Bronze
11 12 16 8 Metal - Contaminated
11 12 16 8 Metal - Copper
11 12 16 8 Metal - Iron
11 12 16 9 Metal - Brass
11 12 16 9 Metal - Bronze
11 12 16 9 Metal - Contaminated
11 12 16 9 Metal - Copper
11 12 16 9 Metal - Iron
11 12 16 10 Metal - Brass
11 12 16 10 Metal - Bronze
11 12 16 10 Metal - Contaminated
11 12 16 10 Metal - Copper
11 12 16 10 Metal - Iron
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
1. Insert data into temp table with an identifier, (alternatively you could add an identity column to the current table too).
SELECT ROW_NUMBER() OVER(ORDER BY DESCRIPTION) RowID,
HeadId,
AreaId,
BranchId,
SiteId,
Description
INTO #NEW_TEMP_TABLE
2. Loop through the data (I am not a fan of CURSORS, this method is MUCH faster)
Declare @CurrenRowtID int
Declare @LastRowtID int
SELECT @CurrenRowtID = 0
SELECT @LastRowtID = Max(RowID)
FROM #NEW_TEMP_TABLE
SELECT Top 1 @CurrenRowtID = RowID
FROM #NEW_TEMP_TABLE
WHERE RowID > @CurrenRowtID
ORDER BY RowID
WHILE @CurrenRowtID <= @LastRowtID
BEGIN
-- Do your work Here
SELECT Top 1 @CurrenRowtID = RowID
FROM #NEW_TEMP_TABLE
WHERE RowID > @CurrenRowtID
ORDER BY RowID
END