use master go if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[uspMaxLengths]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[uspMaxLengths] GO create procedure uspMaxLengths( @TableName varchar(200)) as declare @columnName varchar(200) declare @sql varchar(500) declare columnCursor cursor local for select column_name from information_schema.columns where table_name = @TableName and (data_type = 'varchar' or data_type= 'nvarchar') open columnCursor fetch next from columnCursor into @columnName set nocount on WHILE @@FETCH_STATUS = 0 begin set @sql = 'select ''' + @columnName + ' / '', max( len( isnull( [' + @columnName + '], ''''))) from ' + @TableName print @sql exec( @sql) fetch next from columnCursor into @columnName end close columnCursor deallocate columnCursor GO