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 8831151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:14:52+00:00 2026-06-14T08:14:52+00:00

I have a table that I’m trying to break out each row into one

  • 0

I have a table that I’m trying to break out each row into one or more rows based on the second column value. Like this:

table (id, pcs):
ABC   3
DEF   1
GHJ   4

query result (id, pcs_num):
ABC   1
ABC   2
ABC   3
DEF   1
GHJ   1
GHJ   2
GHJ   3
GHJ   4

I’m writing this as a sproc in SQL server 2008. My best solution is to use a cursor and add [pcs] number of rows to a temp table for each row in the table. Is seems like there must be a simpler solution than this that I am missing. Thanks.

  • 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-06-14T08:14:54+00:00Added an answer on June 14, 2026 at 8:14 am

    You can use a recursive CTE:

    ;WITH CTE AS
    (
        SELECT *
        FROM YourTable
        UNION ALL 
        SELECT id, pcs-1
        FROM CTE
        WHERE pcs-1 >= 1
    )
    SELECT *
    FROM CTE
    ORDER BY id, pcs
    OPTION(MAXRECURSION 0)
    

    Here is a demo for you to try.

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

Sidebar

Related Questions

I have a table that has a number of columns. For each row, I'd
I have table that contains more than 12 millions of rows. I need to
I have a table that has two columns that each contain a member id
I have one table that holds my ressources: Ressource | Ressource-ID And a table
I have a table that inserts a row when a certain function is used
How to select all records from one table that have corespondent keys with other,
I have a table that displays 10 rows per page. Now, given an item's
I have a table that currently has two columns per row and I need
I have table that shows some records from database. each table has a column
I have a table that contains for each entry an incident_id , status (Let's

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.