表中的75列-我只想查询该表中的前25列,而不必命名每个列的名称....您可以协助进行SQL查询....

我一直在玩以下游戏:

Select Table_Name, Count(*) As ColumnCount
From   Information_Schema.Columns
Group By Table_Name
Order By Table_Name

不符合我的输出........

如果表有75列,如何在不命名每个列名称的情况下查看前25列?不想删除列只想查看同一表中75列中的前25列..... TOP未启用,需要其他解决方法...。

最佳答案

表查询的前25列内置于@query中,然后执行。替换正确的@target_table值。

DECLARE
    @target_table sysname
,   @query nvarchar(max)

SET
    @target_table = '_dimAreaOverlay'

; with of_interest as
(
SELECT
    SS.name AS schemaname
,   T.name AS tablename
,   SC.name AS columname
FROM
    sys.schemas SS
    inner join
        sys.tables T
        ON T.schema_id = SS.schema_id
    inner join
        sys.columns SC
        ON SC.object_id = T.object_id
WHERE
    T.name = @target_table
    AND SC.column_id < 26
)
, c AS
(
    SELECT
        STUFF((
        SELECT
            ',' + QUOTENAME(I.columname)
        FROM
            of_interest I
        FOR XML PATH('')), 1,1, '') AS column_list
    ,   OI.tablename
    ,   OI.schemaname
    FROM
        of_interest OI
    GROUP BY
        OI.schemaname
    ,    OI.tablename
)
SELECT
    @query = 'SELECT '
    + C.column_list
    + ' FROM '
    + QUOTENAME(C.schemaname)
    + '.'
    + QUOTENAME(C.tablename)
FROM C

EXECUTE(@query)

关于sql - SQL-仅查询该表的前25列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7506774/

10-11 01:22