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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:55:28+00:00 2026-06-15T20:55:28+00:00

I just stuck to update the below table columns. Consider below scripts. declare @Table1

  • 0

I just stuck to update the below table columns. Consider below scripts.

declare @Table1 Table  ( ID int Identity(1,1), UserCount int )
insert into @Table1 (UserCount) values (2),(3),(5)
declare @Table2 Table ( ID int Identity(1,1), Name varchar(10), IDRef int null)
insert into @Table2 (Name) values ('p1'),('p2'),('p3'),('p4'),('p5'),('p6'),('p7'),('p8'),('p9'),('p10')

The Result is

select * from @Table1

ID          UserCount
----------- -----------
1           2
2           3
3           5

select * from @Table2

ID          Name       IDRef
----------- ---------- -----------
1           p1         NULL
2           p2         NULL
3           p3         NULL
4           p4         NULL
5           p5         NULL
6           p6         NULL
7           p7         NULL
8           p8         NULL
9           p9         NULL
10          p10        NULL

Based on the @Table1.UserCount value i need to update the Table2.IDRef value with @Table1.ID.

Expected Result is,

ID          Name       IDRef
----------- ---------- -----------
1           p1         1
2           p2         1
3           p3         2
4           p4         2
5           p5         2
6           p6         3
7           p7         3
8           p8         3
9           p9         3
10          p10        3
  • 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-15T20:55:29+00:00Added an answer on June 15, 2026 at 8:55 pm

    SQL Fiddle

    Query:

    declare @Table1 Table  ( ID int Identity(1,1), UserCount int )
    insert into @Table1 (UserCount) values (2),(3),(5)
    declare @Table2 Table ( ID int Identity(1,1), Name varchar(10), IDRef int null)
    insert into @Table2 (Name) values ('p1'),('p2'),('p3'),('p4'),('p5'),('p6'),('p7'),('p8'),('p9'),('p10')
    
    update t2
    set t2.idref = t1.id
    from (
        select *, rn=row_number() over (order by id)
          from @table1 t1
          join master..spt_values v on v.type='p'
                                   and v.number between 1 and t1.UserCount
    ) t1
    join (select *, rn=row_number() over (order by id)
            from @table2
    ) t2 on t1.rn=t2.rn
    
    select * from @Table2
    order by id
    

    Results:

    | ID | NAME | IDREF |
    ---------------------
    |  1 |   p1 |     1 |
    |  2 |   p2 |     1 |
    |  3 |   p3 |     2 |
    |  4 |   p4 |     2 |
    |  5 |   p5 |     2 |
    |  6 |   p6 |     3 |
    |  7 |   p7 |     3 |
    |  8 |   p8 |     3 |
    |  9 |   p9 |     3 |
    | 10 |  p10 |     3 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've gotten stuck(again) I have a table and one of the columns is a
I am currently trying to insert data into a table called customer_quote , this
I just stuck in a very simple situation bt couldnt help myself.My question is
I just stuck at this and cannot find solution. I would like to try
I am trying to do an app i just stuck with some logic in
I am running a Grails application and it just gets stuck in the Configuring
Just learning Silverlight 4/RIA and i 'm stuck in a weird problem: setup an
Just as the question says, I'm stuck with .Net 2.0 here in my company
I´ve been stuck on a minor jquery problem, it´s probably just something minor I´m
I'm just getting started with Visual Basic .NET and I'm currently stuck on the

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.