Get row number from temp table with loop
-
Thursday, January 24, 2013 2:25 AM
I have a variable that has comma separated values. There can be in number of values in there. I need to loop through the temp table and pull a value from each record. I get a count from the table So for example let's say there are 5 values. Now I need to get the last value which would be the 5th record. Than while @recordcount > 0 then the 4th records value and so on. Here is what I have so far.
DECLARE @dealercount INT
SET @dealercount = (Select COUNT(val) from dbo.udfIntegerArrayListTable(@dealerid))
DECLARE @dealerids int
WHILE @dealercount > 0
BEGIN
SELECT val FROM
(
SELECT Row_Number() OVER (ORDER BY val) as rowid,
val from dbo.udfIntegerArrayListTable(@dealerid)
) as a
where rowid = @dealercount
Alan
All Replies
-
Thursday, January 24, 2013 2:43 AMModerator
The following splitter table-valued function returns an ID number as the first column of a table variable also:
dbo.fnSplitStringListXML
at http://www.sqlusa.com/bestpractices/training/scripts/userdefinedfunction/
You can just take the MAX of ID to find the last element in the list.
Kalman Toth SQL 2008 GRAND SLAM
Paperback: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012- Marked As Answer by anaylor01 Thursday, January 24, 2013 3:40 AM

