如何按对象标识和列标识查询表数据?

时间:2016-04-30 13:15:16

标签: sql sql-server database-schema dynamic-sql

拥有表Clients

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

我想得到:

Desired Table

使用以下查询为我提供前四列,但如何将其与表数据相关联?

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;

2 个答案:

答案 0 :(得分:3)

您需要unpivot数据。

尝试这样的事情

;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 

答案 1 :(得分:0)

另一种方法,在这里你必须将所有列转换为一种类型,例如我将int转换为varchar(50),我表中的所有其他列都是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;

输出:

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