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
Looks like I found answer 🙂
Select Top row of 2nd table in SQL Join