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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:00:36+00:00 2026-06-05T07:00:36+00:00

I am submitting the following query in Sql Server (2008) WITH query AS (SELECT

  • 0

I am submitting the following query in Sql Server (2008)

WITH query AS (SELECT TOP 100 PERCENT
    ROW_NUMBER() OVER
    (ORDER BY [tbl2].[col2] ASC) AS TableRowNumber ,
    [tbl1].[col1] ,
    [tbl2].[col2]
FROM [db1].[dbo].[tbl1] AS [tbl1]
JOIN [db2].[dbo].[tbl2] AS [tbl2]
    ON [tbl1].[id] = [tbl2].[id])

SELECT
    *
FROM query
WHERE TableRowNumber BETWEEN 1 AND 15
ORDER BY TableRowNumber ASC

When this query is run, it returns the following error message:

Msg 207, Level 16, State 1, Line 3
Invalid column name 'col2'. 

The sql join itself runs fine (when run as a normal query. The issue seems to be with using the Row_Number() over (Order By COLUMN) when COLUMN is in a different database from the FROM table of the query.

If I would change line 3 to read (Order By [tbl1].[col1] ASC) then it runs without any issues. The error only happens when the sort column is in a different DB.

Does anyone know why this is happening? Any suggestions on how to fix this?

  • 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-05T07:00:38+00:00Added an answer on June 5, 2026 at 7:00 am

    This works for me no problem:

    SELECT a.name, b.object_id, rn = ROW_NUMBER() OVER (ORDER BY b.object_id DESC)
      FROM sys.columns AS a 
      INNER JOIN tempdb.sys.objects AS b
      ON a.object_id = b.object_id;
    

    So I suspect there is some other issue going on (e.g. col2 really doesn’t exist). Also I noticed that you are calling the thing tb2 and tbl2 – is it possible you have both a tb2 and a tbl2 in the other database, and you’re referencing the wrong one?

    EDIT I created this:

    CREATE DATABASE db1;
    GO
    USE db1;
    GO
    CREATE TABLE dbo.tbl1(ID INT, col1 INT);
    GO
    INSERT dbo.tbl1 SELECT 1, 5
    UNION ALL SELECT 2, 10;
    GO
    CREATE DATABASE db2;
    GO
    USE db2;
    GO
    CREATE TABLE dbo.tbl2(ID INT, col2 INT);
    GO
    INSERT dbo.tbl2 SELECT 1, 9
    UNION ALL SELECT 2, 4;
    GO
    USE db1;
    GO
    

    Then ran your query in the context of db1. It ran fine. So for the last time I will suggest that there is something you’re not telling us about the schema, or perhaps the fact that you’ve obfuscated the names (and already had to correct one typo from doing so) has obfuscated something too much even for you…

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

Sidebar

Related Questions

I'm using SQL Server 2008, I'm importing the data using the Import Wizard. I
I have following PHP loop + SQL Update query. for ($i=0;$i<count($_POST['id']);$i++) { if(!isset($_POST['live'][$i])){ $_POST['live'][$i]
I'm submitting to a rails webservice the following message: xmlPostData = <message> <message-text> +
I faced the following issue while I submitting my form using jQuery FORM and
I have following small script to preview some text before submitting it to store
For some reason the following form isn't submitting the file like it should and
I am getting Error in submitting the form using AJAX and Jquery, following is
I have the following which I use for submitting forms on my site: <a
I am using the following code. Which is submitting fine with php and it
After submitting our application several times, we continue to receive the following response: Thank

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.