Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 20th, 2004, 05:27 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default Distinct Column Occurrences

Hi all,

I am looking for some help in find out all distinct column values in my database. the select statement below gives me the result I need but I need to be able scan all values in each column of every table.
Code:
SELECT COUNT(DISTINCT state) as states,
       COUNT(DISTINCT city) as cities,
       address.zipcode 
FROM address, addr_prop_jct, property
WHERE address.addr_nbr=addr_prop_jct.addr_nbr AND 
      property.folio_nbr=addr_prop_jct.folio_nbr 
GROUP BY address.zipcode
Results
2 4 NULL
1 1
1 1 34105-
0 0 34117-
2 2 34119-
0 0 34120-
1 1 44646-


Jaime E. Maccou
Applications Analyst
__________________
Jaime E. Maccou
 
Old October 24th, 2004, 03:23 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Hi all,
For those of you who may be interested in occurrences and distinct counts. This store procedure will scan your default database and return each column value for every table.

Note: It takes a long time to run depending on how much data you have in your database.
Code:
create procedure  sp_tableinfo
as
BEGIN
DECLARE @table sysname
DECLARE @column sysname
DECLARE @datatype sysname
declare @col_value sysname
DECLARE @data_type_length sysname

DECLARE @sql nvarchar(4000)

SET NOCOUNT ON 
--EXEC sp_updatestats

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED -- Will speed things up a bit

CREATE TABLE table_infoname 
(table_name sysname NOT NULL
,column_name sysname NOT NULL
,datatype sysname NOT NULL
,data_type_length sysname  null
,col_value varchar(7000) NULL
)


DECLARE c CURSOR FAST_FORWARD FOR 
SELECT 
isc.table_name, 
isc.column_name, 
isc.data_type, 
COALESCE(isc.character_maximum_length, isc.numeric_precision) as data_type_length
FROM information_schema.columns isc
INNER JOIN information_schema.tables ist
ON isc.table_name = ist.table_name
WHERE ist.table_type = 'base table'
AND ist.table_name not like 'dt%'
ORDER BY 1,2

set @sql = ''
OPEN c
FETCH NEXT FROM c INTO @table, @column, @datatype,@data_type_length
WHILE @@FETCH_STATUS = 0
BEGIN
     IF @datatype NOT IN ('text', 'ntext', 'image')
     BEGIN
          SET @sql = 'INSERT INTO table_infoname SELECT DISTINCT ''' + @table + ''', ''' + @column +''',''' + @datatype + ''',''' + @data_type_length + ''', convert(varchar(7000),[' + @column + ']) FROM [' + @table + ']'
     END
     ---PRINT @sql
     EXEC(@sql)
     FETCH NEXT FROM c INTO @table, @column,@datatype,@data_type_length
END
CLOSE c
DEALLOCATE c
end
Jaime E. Maccou
Applications Analyst
 
Old October 25th, 2004, 04:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Jamie,

I couldn't sit and work on that continuously.

That was really a good effort.

Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening distinct files in a database Link Column gene4455 BOOK: Beginning ASP 3.0 9 November 13th, 2008 03:16 PM
XSLT Code for Multiple occurrences vijayp2p XSLT 1 May 9th, 2006 11:23 AM
Distinct SELECT DISTINCT question... EndEffect Classic ASP Databases 4 August 18th, 2005 08:53 AM
xslt with multiple occurrences in a record Olaf_l XSLT 2 April 7th, 2005 07:02 AM
Select distinct data from one column madkaikar_ashish SQL Server 2000 1 December 17th, 2004 10:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.