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 9186333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:30:02+00:00 2026-06-17T19:30:02+00:00

I am trying to get a list of all columns from a table with

  • 0

I am trying to get a list of all columns from a table with their data types, data lengths and the length of the longest value in that column.

I have this SQL for getting the columns and their data types and lengths:

SELECT 
    Object_Name(c.object_id),
    c.name 'Column Name',
    t.Name 'Data type',
    c.max_length 'Max Length'
FROM    
    sys.columns c
INNER JOIN 
    sys.types t ON c.system_type_id = t.system_type_id
WHERE
    c.object_id = OBJECT_ID('MyTable')

And I have this SQL for getting the maximum length of a value:

SELECT Max(Len(MyColumn))
FROM MyTable

But I can’t figure out how to combine them. I am using SQL Server 2008.

  • 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-06-17T19:30:04+00:00Added an answer on June 17, 2026 at 7:30 pm

    Thanks for the suggestions. I have come up with the following solution. It gets me the data i need but would be interested to see if it can be made more efficient.

    declare @results table
    (
    ID varchar(36),
    TableName varchar(250),
    ColumnName varchar(250),
    DataType varchar(250),
    MaxLength varchar(250),
    Longest varchar(250),
    SQLText varchar(250)
    )
    
    INSERT INTO @results(ID,TableName,ColumnName,DataType,MaxLength,Longest,SQLText)
    SELECT 
        NEWID(),
        Object_Name(c.object_id),
        c.name,
        t.Name,
        case 
            when t.Name != 'varchar' Then 'NA'
            when c.max_length = -1 then 'Max' 
            else CAST(c.max_length as varchar)
        end,
        'NA',
        'SELECT Max(Len(' + c.name + ')) FROM ' + OBJECT_SCHEMA_NAME(c.object_id) + '.' + Object_Name(c.object_id)
    FROM    
        sys.columns c
    INNER JOIN 
        sys.types t ON c.system_type_id = t.system_type_id
    WHERE
        c.object_id = OBJECT_ID('MyTable')    
    
    
    DECLARE @id varchar(36)
    DECLARE @sql varchar(200)
    declare @receiver table(theCount int)
    
    DECLARE length_cursor CURSOR
        FOR SELECT ID, SQLText FROM @results WHERE MaxLength != 'NA'
    OPEN length_cursor
    FETCH NEXT FROM length_cursor
    INTO @id, @sql
    WHILE @@FETCH_STATUS = 0
    BEGIN
        INSERT INTO @receiver (theCount)
        exec(@sql)
    
        UPDATE @results
        SET Longest = (SELECT theCount FROM @receiver)
        WHERE ID = @id
    
        DELETE FROM @receiver
    
        FETCH NEXT FROM length_cursor
        INTO @id, @sql
    END
    CLOSE length_cursor
    DEALLOCATE length_cursor
    
    
    SELECT 
        TableName, 
        ColumnName, 
        DataType, 
        MaxLength, 
        Longest 
    FROM 
        @results
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a list of all the users from users table and
I'm trying to get a list of multiple columns from my table using QueryDSL,
I'm trying to get all records from a table and loop through it. Pseudo
Trying to get a list of all substrings currently I have a function but
For a blogging app, I'm trying to get a list of all months, in
I am trying to get the code to list all the directories in a
I'm trying to get a list in JavaScript (not using jQuery) of all the
I am trying to do a simple query to get a list of all
I'm trying to parse this XML I want to get a list of all
Here's what I'm trying to do: I want to get a list of all

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.