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

  • Home
  • SEARCH
  • 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 6030111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:59:17+00:00 2026-05-23T04:59:17+00:00

How do I go about doing the following? I am using the following query

  • 0

How do I go about doing the following?

I am using the following query to get a specific users tab ids:

select id
from intranet.dbo.tabs
where cms_initials = @user
order by id asc

which might return the following ids

4
5
6
7

I now want to insert the rows from the following query:

select tabs_id, widgets_id, sort_column, sort_row
from intranet.dbo.columns c
inner join intranet.dbo.tabs t on c.tabs_id = t.id
where t.is_default = 1

But use the ids from the first query to replace the tab ids

so if the second query originally returns tabs_id’s as

0
0
0
0
1
1
1
2
2
2
3
3

I should end up with

0
0
0
0
1
1
1
2
2
2
3
3
4
4
4
4
5
5
5
6
6
6
7
7

Is this possible with sql server 2005 without using stored procedures?

So far I have

insert into intranet.dbo.columns ( tabs_id, widgets_id, sort_column, sort_row )
select tabs_id, widgets_id, sort_column, sort_row
    from intranet.dbo.columns c
    inner join intranet.dbo.tabs t on c.tabs_id = t.id
    where t.is_default = 1

But this just copies everything as is, I need to do that, but replace the ids in the copied rows.

  • 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-23T04:59:18+00:00Added an answer on May 23, 2026 at 4:59 am

    This solution uses common table expressions and ranking functions. A and B are your original queries ranked by tab order. A and B are then joined by tab ranking and inserted.

    USE intranet
    
    ;WITH A AS
    (
        SELECT ROW_NUMBER() OVER (ORDER BY id) AS tab_ranking
            , id
        FROM dbo.tabs
        WHERE cms_initials = @user
    ),
    B AS
    (
        SELECT DENSE_RANK() OVER (ORDER BY tabs_id) AS tab_sequence
            , tabs_id, widgets_id, sort_column, sort_row
        FROM dbo.columns
        WHERE tabs_id IN (SELECT t.id FROM dbo.tabs t WHERE t.is_default = 1)
    )
    INSERT INTO dbo.columns (tabs_id, widgets_id, sort_column, sort_row)
    SELECT a.id, b.widgets_id, b.sort_column, b.sort_row
    FROM A
    INNER JOIN B ON B.tab_ranking = A.tab_ranking
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know almost nothing about linq. I'm doing this: var apps = from app
I've got a Doctrine_RawSql query using prepared statements. However, they seem to get ignored
PostgreSQL is about to make me punch small animals. I'm doing the following SQL
I am having trouble doing something simple like the following using (SPSite site =
How do you go about doing a daily build and striving for a zero-defect
I've been doing some reading about the people building an emulator for the Wii
About a half dozen times now, I’ve been doing a regular chore in Visual
I've been doing a lot of reading about AJAX, and wanted to know which
I'm doing Cay Horstmann's combinator parser exercises, I wonder about the best way to
I'm about to start a project where I will be the only one doing

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.