Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:33:59+00:00 2026-05-27T13:33:59+00:00

Using Microsoft SQL 2008 I have a loop that should grab data from each

  • 0

Using Microsoft SQL 2008
I have a loop that should grab data from each row but it only seems to be grabbing the last row.
The table is

CREATE TABLE [dbo].[TEST](
    [PK] [int] NULL,
    [ITEMS] [varchar](50) NULL
) ON [PRIMARY]

insert into dbo.test (pk, items) values (1, '1111')
insert into dbo.test (pk, items) values (2, '2222')
insert into dbo.test (pk, items) values (3, '3333')
insert into dbo.test (pk, items) values (4, '4444')

I am trying to loop through it and grab the items per row for an insert but I am only getting the last row:

-- Get the number of rows in the looping table
DECLARE @RowCount INT
SET @RowCount = (SELECT COUNT(ITEMS) FROM dbo.TEST) 

Print @ROWCOUNT

-- Declare an iterator
DECLARE @I INT
-- Initialize the iterator
SET @I = 1

--Loop through the rows of a table 
WHILE (@I <= @RowCount)

BEGIN

Declare @Name varchar(100)

SELECT @Name = Lower(ITEMS) FROM dbo.TEST


PRINT 'Row No = ' + CONVERT(VARCHAR(2), @I)

Print @Name

-- Increment the iterator
        SET @I = @I  + 1

END

The results I am getting are:

4
Row No = 1
4444
Row No = 2
4444
Row No = 3
4444
Row No = 4

Seems simple enough but I can’t find any information on why this is happening and/or get my head around the loop and why it is not working.
Thank you,
Heather

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:34:00+00:00Added an answer on May 27, 2026 at 1:34 pm

    As Philip Kelley noted SELECT @Name = Lower(ITEMS) FROM dbo.TEST will always give you the last item in the result. So his solution (adding WHERE PK = @I) will work if the PKs in the table start with 1 and are sequential. If either those conditions don’t hold true then you’ll have to do somthing like this.

    ;WITH CTE as 
    (SELECT row_number() over (order by pk) rn, items
    FROM dbo.TEST)
    SELECT @Name = Lower(items) FROM CTE where rn = @i
    

    Although in all honestly you’re probably better of with FAST_FORWARD cursor since you’ve lost the set-based operations battle already.

    See this data.se query for a working example (note I added another item 6,'6666')

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website using Microsoft SQL 2008 server over local network. Sometimes, SQL
I've got a database that is mirrored using SQL 2008 Mirroring. I have a
I am using T/SQL in Microsoft SQL Server 2008 I have a table CREATE
I'm using Microsoft SQL Server 2008. I'm not an expert with it but I
I am using playframework with a legacy SQL Server 2008 database that has data
I'm using Microsoft SQL Svr Mgmt Studio 2008. I don't have access to create
I am using Esri ArcMap 10.0 with Microsoft Sql Server 2008 R2 I have
Using Microsoft SQL Server 2008. I have a table of interest rates. I've manually
I am using Microsoft SQL Server 2008. I have a stored procedure. Is there
I am using Microsoft SQL Server 2008. I have a very large text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.