MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 1st, 2012, 11:10 AM
erik80 erik80 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 1 erik80 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 34 m 18 sec
Reputation Power: 0
Best way to get the max length in every column in a table

hello,

i'd like a query that goes through every column in a table and returns the max length of data in them. i know how to do both separately, just not sure how to join them together and show the column name and the max length within next to each other. sometimes when loading data i run into truncation errors going from the raw table to the process table, and rather than go through fiddling with the lengths in access of 100+ columns, it would be far more efficient to see exactly which column the problem lies in to correct it.

thanks in advance,

erik

Reply With Quote
  #2  
Old March 9th, 2012, 07:17 PM
wordracr wordracr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 608 wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 49 m 10 sec
Reputation Power: 14
You're just looking for some way to automatically generate the query, right?

declare @sql varchar(max)
select @sql = @sql + ', max(length(' + column_name + ')) as Max' + column_name
from information_schema.columns
where table_name = 'YourTable'
-- untested, but needed to only target text columns:
-- and type in ('varchar', 'nvarchar' etc...)

select @sql = 'select ' + substring(@sql, 1) /* take out first comma */ + ' from YourTable'
sp_ExecuteSQL @sql

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Best way to get the max length in every column in a table

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap