How do I return the SQL data types from my query?

前端 未结 13 1457
耶瑟儿~
耶瑟儿~ 2021-01-29 23:58

I\'ve a SQL query that queries an enormous (as in, hundreds of views/tables with hard-to-read names like CMM-CPP-FAP-ADD) database that I don\'t need nor want to understand. Th

相关标签:
13条回答
  • 2021-01-30 00:23
    SELECT COLUMN_NAME,
           DATA_TYPE,
           CHARACTER_MAXIMUM_LENGTH
    FROM information_schema.columns
    WHERE TABLE_NAME = 'YOUR_TABLE_NAME'
    

    You can use columns aliases for better looking output.

    0 讨论(0)
  • 2021-01-30 00:23

    Can you get away with recreating the staging table from scratch every time the query is executed? If so you could use SELECT ... INTO syntax and let SQL Server worry about creating the table using the correct column types etc.

    SELECT *
    INTO your_staging_table
    FROM enormous_collection_of_views_tables_etc
    
    0 讨论(0)
  • 2021-01-30 00:24

    I use a simple case statement to render results I can use in technical specification documents. This example does not contain every condition you will run into with a database, but it gives you a good template to work with.

    SELECT
         TABLE_NAME          AS 'Table Name',
         COLUMN_NAME         AS 'Column Name',
         CASE WHEN DATA_TYPE LIKE '%char'
              THEN DATA_TYPE + '(' + CONVERT(VARCHAR, CHARACTER_MAXIMUM_LENGTH) + ')'
              WHEN DATA_TYPE IN ('bit', 'int', 'smallint', 'date')
              THEN DATA_TYPE
              WHEN DATA_TYPE = 'datetime'
              THEN DATA_TYPE + '(' + CONVERT(VARCHAR, DATETIME_PRECISION) + ')'
              WHEN DATA_TYPE = 'float'
              THEN DATA_TYPE
              WHEN DATA_TYPE IN ('numeric', 'money')
              THEN DATA_TYPE + '(' + CONVERT(VARCHAR, NUMERIC_PRECISION) + ', ' + CONVERT(VARCHAR, NUMERIC_PRECISION_RADIX) + ')'
         END                 AS 'Data Type',
         CASE WHEN IS_NULLABLE = 'NO'
              THEN 'NOT NULL'
              ELSE 'NULL'
         END                 AS 'PK/LK/NOT NULL'
    FROM INFORMATION_SCHEMA.COLUMNS 
    ORDER BY 
         TABLE_NAME, ORDINAL_POSITION
    
    0 讨论(0)
  • 2021-01-30 00:28

    sp_describe_first_result_set

    will help to identify the datatypes of query by analyzing datatypes of first resultset of query

    https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-describe-first-result-set-transact-sql?view=sql-server-2017

    0 讨论(0)
  • 2021-01-30 00:29

    There MUST be en easier way to do this... Low and behold, there is...!

    "sp_describe_first_result_set" is your friend!

    Now I do realise the question was asked specifically for SQL Server 2000, but I was looking for a similar solution for later versions and discovered some native support in SQL to achieve this.

    In SQL Server 2012 onwards cf. "sp_describe_first_result_set" - Link to BOL

    I had already implemented a solution using a technique similar to @Trisped's above and ripped it out to implement the native SQL Server implementation.

    In case you're not on SQL Server 2012 or Azure SQL Database yet, here's the stored proc I created for pre-2012 era databases:

    CREATE PROCEDURE [fn].[GetQueryResultMetadata] 
        @queryText VARCHAR(MAX)
    AS
    BEGIN
    
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        --SET NOCOUNT ON;
    
        PRINT @queryText;
    
        DECLARE
                    @sqlToExec NVARCHAR(MAX) = 
                        'SELECT TOP 1 * INTO #QueryMetadata FROM ('
                        +
                        @queryText
                        +
                        ') T;'
                        + '
                            SELECT
                                        C.Name                          [ColumnName],
                                        TP.Name                         [ColumnType],
                                        C.max_length                    [MaxLength],
                                        C.[precision]                   [Precision],
                                        C.[scale]                       [Scale],
                                        C.[is_nullable]                 IsNullable
                            FROM
                                        tempdb.sys.columns              C
                                            INNER JOIN
                                        tempdb.sys.types                TP
                                                                                    ON
                                                                                            TP.system_type_id = C.system_type_id
                                                                                                AND
                                                                                            -- exclude custom types
                                                                                            TP.system_type_id = TP.user_type_id
                            WHERE
                                        [object_id] = OBJECT_ID(N''tempdb..#QueryMetadata'');
                '
    
        EXEC sp_executesql @sqlToExec
    
    END
    
    0 讨论(0)
  • 2021-01-30 00:29

    This easy query return a data type bit. You can use this thecnic for other data types:

    select CAST(0 AS BIT) AS OK

    0 讨论(0)
提交回复
热议问题