Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 705929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:06:34+00:00 2026-05-14T04:06:34+00:00

Given some arbitrary SQL I would like to get the data types of the

  • 0

Given some arbitrary SQL I would like to get the data types of the returned columns. The statement might join many tables, views, TVFs, etc. I know I could create a view based on the query and get the datatypes from that, hoping there’s a quicker way. Only think I’ve been able to think of is writing a .net utility to run the SQL and examine the results, wondering if there is a TSQL answer.


i.e.

Given (not real tables just an example)

SELECT p.Name AS PersonName, p.Age, a.Account as AccountName
FROM Person as p
LEFT JOIN Account as a
    ON p.Id = a.OwnerId

I would like to have something like

PersonName: (nvarchar(255), not null)

Age: (smallInt, not null)

etc…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:06:34+00:00Added an answer on May 14, 2026 at 4:06 am
        /*you may have to alias some columns if they are not unique*/
        /*EDIT: added fix for 2byte nchar/nvarchar */
        SELECT top (1)  /*<your query columns here>*/ 
        INTO #tmp99
        /*<rest of your query here>*/
    
    
        SELECT 'CREATE TABLE [tablename]('  UNION ALL 
        SELECT CASE WHEN ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) = 1 THEN '' ELSE ',' END 
        + CHAR(13) + '[' + c.name + '] [' + t.name + ']' 
        + CASE WHEN c.system_type_id IN (165,167,173,175) 
               THEN CASE WHEN c.max_length <> -1
                         THEN '(' + CAST(c.max_length AS varchar(7)) + ')' 
                         ELSE '(max)' 
                         END 
               WHEN c.system_type_id IN (231,239) 
               THEN CASE WHEN c.max_length <> -1 
                         THEN '(' + CAST(c.max_length/2 AS varchar(7)) + ')' 
                         ELSE '(max)' END
               ELSE 
                   CASE WHEN c.system_type_id IN (41,42,43) 
                        THEN '(' + CAST(c.scale AS varchar(7)) + ')'
                        ELSE
                            CASE WHEN c.system_type_id IN (106,108) 
                                 THEN '(' + CAST(c.precisiON AS varchar(7)) + ',' + CAST(c.scale AS varchar(7)) + ')'
                                 ELSE ''
                                 END 
                        END
               END
    
        + CASE WHEN c.is_nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END 
    
        FROM tempdb.sys.columns c
        JOIN tempdb..sysobjects o ON (c.object_id = o.id)
        JOIN tempdb.sys.types t ON (t.user_type_id = c.user_type_id)
        WHERE o.name LIKE '#tmp99%' 
        UNION ALL SELECT ')' 
        FOR XML PATH('')
    
        DROP TABLE #tmp99
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The culprit in this case was CreateCompatibleBitmap. Apparently Windows may… May 14, 2026 at 6:36 pm
  • Editorial Team
    Editorial Team added an answer Others have provided good answers; I'll summarize and add one… May 14, 2026 at 6:36 pm
  • Editorial Team
    Editorial Team added an answer If you are just trying to get a copy of… May 14, 2026 at 6:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.