Hi all,I have a follwing cursor that i will like to change.I will like to get all of the different occurrences in a columnThe bold area is where I will like to make the change but I cannot figure it out. Any help be greatly appreciated.Posted - 04/14/2003 : 06:33:27 -------------------------------------------------------------------------------- /*This script gives a list of all the columns in the database and the number of distinct values in them. This can be very useful if you take over a database and you want to find out if the columns are actually used. It might also come in handy if you need some information about the cardinality of your columns to help you design indexes.It also gives information about the largest size of data in a column and the actual size of the column, so you can find out if those text columns are really necessary for example.Although the script includes a few performance optimisations, it is not advisable to run it against a production database, as it will perform a table scan on all the tables multiple times.Jacco */create procedure sp_tableinfoasDECLARE @table sysnameDECLARE @column sysnameDECLARE @datatype sysnameDECLARE @designed_length intDECLARE @all_count intDECLARE @sql nvarchar(4000)SET NOCOUNT ON EXEC sp_updatestatsSET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED -- Will speed things up a bitCREATE TABLE #table_info (table_name sysname NOT NULL,column_name sysname NOT NULL,data_type sysname NOT NULL,designed_length int NULL,max_length int NULL,distinct_count int NULL,all_count int NOT NULL,cardinality AS CASE WHEN distinct_count IS NULL THEN CAST(data_type AS varchar(7))WHEN all_count = 0 THEN CAST('No rows' AS varchar(7))ELSE CAST(CAST(CAST(distinct_count AS decimal)/CAST(all_count AS decimal) AS decimal(18,4)) AS varchar(7))END)DECLARE c CURSOR FAST_FORWARD FOR SELECT isc.table_name, isc.column_name, isc.data_type, COALESCE(isc.character_maximum_length, isc.numeric_precision),si.rowcntFROM information_schema.columns iscINNER JOIN information_schema.tables istON isc.table_name = ist.table_nameINNER JOIN sysindexes siON isc.table_name = OBJECT_NAME(si.id)WHERE ist.table_type = 'base table'AND ist.table_name not like 'dt%'AND si.indid IN (0,1)ORDER BY isc.table_name, isc.column_nameOPEN cFETCH NEXT FROM c INTO @table, @column, @datatype, @designed_length, @all_countWHILE @@FETCH_STATUS = 0BEGINIF @datatype IN ('text', 'ntext', 'image')BEGINSET @sql = 'SELECT ''' + @table + ''', ''' + @column + ''', ''' + @datatype + ''''SET @sql = @sql + ', ' + CAST(@designed_length AS varchar(10)) + ', MAX(DATALENGTH([' + @column + ']))'SET @sql = @sql + ', NULL' + ', ' + CAST(@all_count AS varchar(10)) + ' FROM [' + @table + ']'ENDELSEBEGINSET @sql = 'SELECT ''' + @table + ''', ''' + @column + ''', ''' + @datatype + ''''SET @sql = @sql + ', ' + CAST(@designed_length AS varchar(10)) + ', MAX(LEN(CAST([' + @column + '] AS VARCHAR(8000))))'SET @sql = @sql + ', COUNT(DISTINCT [' + @column + '])'SET @sql = @sql + ', ' + CAST(@all_count AS varchar(10)) + ' FROM [' + @table + ']'ENDPRINT @sqlINSERT INTO #table_info (table_name, column_name, data_type, designed_length, max_length, distinct_count, all_count)EXEC(@sql)FETCH NEXT FROM c INTO @table, @column, @datatype, @designed_length, @all_countENDCLOSE cDEALLOCATE cSELECT table_name, column_name, data_type, designed_length, max_length, distinct_count, all_count, cardinalityFROM #table_infoSET TRANSACTION ISOLATION LEVEL READ COMMITTEDDROP TABLE #table_info