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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:41:24+00:00 2026-06-01T06:41:24+00:00

consider this CTE ;WITH Columns AS ( SELECT object_id AS TableId, Name AS ColumnName

  • 0

consider this CTE

;WITH Columns AS 
(
    SELECT object_id AS TableId, Name AS ColumnName 
    FROM SYS.columns
), 
Tables AS 
(
    SELECT S.NAME AS SchemaName, T.NAME AS TableName, object_id AS TableId
    FROM sys.tables T
    INNER JOIN  sys.schemas S ON S.schema_id = T.schema_id
)
SELECT T.SchemaName, T.TableName, C.ColumnName
FROM Tables T
INNER JOIN Columns C ON T.TableId = C.TableId

What I want to do is to limit the results of the columns to only be the first three (say ordered alphabetically for example)

So if I have 1 schema (e.g. dbo) with 2 tables TableX & TableY with 4 cols each ColA, ColB, ColC, ColD my results would be
something like

  dbo   TableX    ColA
  dbo   TableX    ColB
  dbo   TableX    ColC
  dbo   TableY    ColA
  dbo   TableY    ColB
  dbo   TableY    ColC

schema_a

  • 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-01T06:41:26+00:00Added an answer on June 1, 2026 at 6:41 am

    Looks like I found answer 🙂

    Select Top row of 2nd table in SQL Join

    ;WITH Columns AS 
    (
        SELECT 
            row_number() over (partition by object_id ORDER BY Name DESC) as RowNumber,
            object_id AS TableId, 
            Name AS ColumnName 
        FROM SYS.columns
    ), 
    Tables AS 
    (
        SELECT 
            S.NAME AS SchemaName, 
            T.NAME AS TableName, object_id AS TableId
        FROM sys.tables T
        INNER JOIN  sys.schemas S ON S.schema_id = T.schema_id
    )
    SELECT T.SchemaName, T.TableName, C.RowNumber, C.ColumnName
    FROM Tables T
    INNER JOIN Columns C ON T.TableId = C.TableId
    where RowNumber BETWEEN 1 AND 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a table or CTE structured like this: Name Num ---- ---- Abc 12
Consider this: //members is a table. String sql=select * from members; Query query =
Consider this var source = new{ Id = 1, Name = Name1} It works
Consider this HTML: <form id=MyForm> <input name=Input1/> </form> In Script# I do this: FormElement
Consider this XML: <Employees> <Person> <ID>1000</ID> <Name>Nima</Name> <LName>Agha</LName> </Person> <Person> <ID>1001</ID> <Name>Ligha</Name> <LName>Ligha</LName> </Person>
Consider this snippet of Jade: if(#{episode[question.name][field]}) #{episode[question.name][field]} I want to check if the value
Consider this HTML: <form id=MyForm> <input name=Input1/> </form> To get the form, I would
Consider this XML: <Employees> <Person> <ID>1000</ID> <Name>Nima</Name> <LName>Agha</LName> </Person> <Person> <ID>1001</ID> <Name>Ligha</Name> <LName>Ligha</LName> </Person>
Consider this simple class that demonstrates RAII in C++ (From the top of my
Consider this piece of XML: <ListBox x:Name=pictureBox ItemsSource={Binding} MouseDoubleClick=item_DoubleClick > Is there any way

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.