根据数据类型筛选表中的列

时间:2017-02-01 20:24:45

标签: sql

表名:表1

Name             DataType
PK_tablekey        INT
FK_Table2Keu       INT
CreateTime       datetime
DateField1       datetime
DateField2       datetime
Createdby        NVARCHAR(32)
Name1             NVARCHAR(32)
Flag             BIT
Value1           Decimal(19,4)
Value2           Decimal(19,4)

我想使用over by子句

仅查询这些字段
DateField1       datetime
DateField2       datetime
Name1            NVARCHAR(32)
Flag             BIT
Value1           Decimal(19,4)
Value2           Decimal(19,4)

查询:

SELECT 
    (ROW_NUMBER() OVER (ORDER BY ORDINAL_POSITION ASC)) AS ColumnNumber,
    CAST(COLUMN_NAME AS NVARCHAR(150)) AS ColumnName    
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_NAME = N'Table1'
    AND DATA_TYPE IN ( N'decimal') 

UNION ALL                      

SELECT  
    (ROW_NUMBER() OVER (ORDER BY ORDINAL_POSITION ASC)) AS ColumnNumber,
    CAST(COLUMN_NAME AS NVARCHAR(150)) AS ColumnName     
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_NAME = N'Table1'
    AND DATA_TYPE IN (N'nvarchar')
    AND COLUMN_NAME = 'Name1 '  

UNION ALL                         

SELECT  
    (ROW_NUMBER() OVER (ORDER BY ORDINAL_POSITION ASC)) AS ColumnNumber,
    CAST(COLUMN_NAME AS datetime) AS ColumnName,
    CAST(COLUMN_NAME AS datetime) AS OverwriteColumnName
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_NAME = N'Table1'
    AND DATA_TYPE IN (N'datetime')
    AND COLUMN_NAME IN ('DateField1', 'DateField2')

当调用此proc时,它似乎不起作用会抛出错误消息

  

请求失败:列名无效:DateField1
  请求失败:列名无效:DateField2

我也想添加BIT列。

如果查询很短,我可以帮助我,因为我使用的是UNION ALL。

2 个答案:

答案 0 :(得分:0)

为什么不这样做?

SELECT ROW_NUMBER() OVER (PARTITION BY DATA_TYPE ORDER BY ORDINAL_POSITION ASC) AS ColumnNumber,
       CAST(COLUMN_NAME AS NVARCHAR(150)) AS ColumnName  
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Table1' AND
      DATA_TYPE IN ( N'decimal', N'datetime', N'Name1') ;

您可能希望指定所需的列,而不是DATA_TYPE。关键的想法是PARTITION BY

答案 1 :(得分:0)

当你使用rownumber时,它的SQL Server。

首先,您的查询有错误:所有联合必须具有相同的列才能返回。你的第三个联合会返回3列而不是必需的两列。

如果您需要订购数据,可以试试这个:

SELECT
     [ColumnNumber] 
    ,[ColumnName]
FROM
(
    SELECT 
         [ColumnNumber] = (ROW_NUMBER() OVER (ORDER BY ORDINAL_POSITION ASC))
        ,[ColumnName]   = CAST(COLUMN_NAME AS NVARCHAR(150)) 
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = N'Table1'
        AND DATA_TYPE IN ( N'decimal') 

    UNION ALL                      

    SELECT  
         [ColumnNumber] =  (ROW_NUMBER() OVER (ORDER BY ORDINAL_POSITION ASC))
        ,[ColumnName]   =  CAST(COLUMN_NAME AS NVARCHAR(150))
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = N'Table1'
        AND DATA_TYPE IN (N'Name1')
        AND COLUMN_NAME = 'Name1 '  

    UNION ALL                         

    SELECT  
         [ColumnNumber] = (ROW_NUMBER() OVER (ORDER BY ORDINAL_POSITION ASC)) 
        ,[ColumnName]   =  CAST(COLUMN_NAME AS datetime) 
        --  , CAST(COLUMN_NAME AS datetime) AS OverwriteColumnName  --you need 2 cols, not 3!
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = N'Table1'
        AND DATA_TYPE IN (N'datetime')
        AND COLUMN_NAME IN ('DateField1','DateField2')
) AS [r]
ORDER BY
     [r].[ColumnNumber]
    ,[r].[ColumnName];