How to query table data by Object Id and Column Id

2019-05-07 08:07发布

问题:

Having the table Clients.

PK  LastName    Name        Address
1   Vidal       Arturo      St....
2   Lavezzi     Ezequiel    St....
3   Cuadrado    Guillermo   St....

I want to get:

With the following query gives me the first four columns but how can I link this with the table data?

SELECT TAB.object_id OBEJCTID, TAB.name TABLENAME, COL.column_id COLUMNID, COL.name FROM sys.tables TAB
JOIN SYS.columns COL
ON TAB.object_id = COL.object_id
WHERE TAB.object_id = 25659888;

回答1:

You need to unpivot the data.

Try something like this

;WITH cte 
     AS (SELECT column_name, 
                table_value 
         FROM   clients 
                CROSS apply (VALUES ('pk',CONVERT(varchar(20),PK)), 
                                    ('LastName',lastname), 
                                    ('Name', NAME), 
                                    ('Address',address)) cs(column_name, table_value)), 
     yourquery 
     AS (SELECT TAB.object_id OBEJCTID, 
                TAB.NAME      TABLENAME, 
                COL.column_id COLUMNID, 
                COL.NAME 
         FROM   sys.tables TAB 
                JOIN sys.columns COL 
                  ON TAB.object_id = COL.object_id 
         WHERE  TAB.object_id = 25659888) 
SELECT * 
FROM   cte c 
       JOIN yourquery y 
         ON y.NAME = c.column_name 


回答2:

Another approach, here you must to convert all columns into one type, for example I convert int into varchar(50), all other columns in my table are varchar(50) eather:

SELECT  TAB.name TableName,
        TAB.object_id ObjectId,
        COL.name ColumnName,
        COL.column_id ColumnId,
        U.ColumnValue TableValue,
        T.name TableType
FROM sys.tables TAB
INNER JOIN SYS.columns COL
    ON TAB.object_id = COL.object_id
INNER JOIN sys.types T
    ON T.user_type_id = COL.system_type_id 
INNER JOIN (
        SELECT ColumnName, ColumnValue
        FROM (SELECT CONVERT(varchar(50),PK) as PK , LastName, Name, [Address] 
            --here to convert all in one type
        FROM Clients)
        AS P
        UNPIVOT
        (
            ColumnValue FOR ColumnName IN (PK, LastName, Name, [Address])
        ) as unpvt 
    ) as U
    ON U.ColumnName = COL.name
WHERE TAB.object_id = 25659888;

Output:

TableName   ObjectId    ColumnName  ColumnId    TableValue  TableType
Clients     25659888    PK          1           1           int
Clients     25659888    LastName    2           Vidal       varchar
Clients     25659888    Name        3           Arturo      varchar
Clients     25659888    Address     4           St....      varchar
Clients     25659888    PK          1           2           int
Clients     25659888    LastName    2           Lavezzi     varchar
Clients     25659888    Name        3           Ezequiel    varchar
Clients     25659888    Address     4           St....      varchar
Clients     25659888    PK          1           3           int
Clients     25659888    LastName    2           Cuadrado    varchar
Clients     25659888    Name        3           Guillermo   varchar
Clients     25659888    Address     4           St....      varchar