Avatar of splanton
splantonFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

creating an iterative loop in a temporary table where there is no ue Primary Key only a candidate key

I have a temporary table that I need to loop through in a function.

A snapshot of the data in the temporary table reveals the following:

@WasteStream
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

Open in new window


All the loop examples I have found on the internet so far have been simple examples that rely on using the primary key from a table as the cursor for the loop. In this case that isn't applicable.

As you can see there is no unique primary key that is going to be of any use, only a candidate key made up of SiteId and Description.

What I need is a loop that will loop through each INDIVIDUAL record in the temporary table and allow me to do the rather complicated processing required for each one (and believe me there is a lot of processing required for each one, hence the loop based solution rather than a set based solution).

Help would be much appreciated.
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
almander
ASKER CERTIFIED SOLUTION
Avatar of JestersGrind
JestersGrind
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of almander
almander

Here is what I would do.
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


Microsoft SQL Server
Microsoft SQL Server

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.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo