Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple Procedure Help

Author  Topic 

ttoker
Starting Member

2 Posts

Posted - 2006-12-20 : 17:25:36
People I am not a SQL Server guy so bear with me please. This should be very simple. I am looking for a procedure; given a TABLE_NAME as input; will run the following three SQL statements for each COLUMN in that table:

select max(COLUMN_NAME) from TABLE_NAME

select count(0) from TABLE_NAME where COLUMN_NAME is null

select count (distinct COLUMN_NAME) from TABLE_NAME

It will then write the output in a readable fashion to a local file.

Thanks a lot

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2006-12-20 : 17:57:19
yeah.. okay, why?

This would be dynamic SQL unless there is some new stuff in 2005 for this (I'm not up-to-date)

dynamic sql is not ideal...

but I havent done this in a while...


Declare @tableName varchar(100),
@sqlStr varchar(8000),
@position int

Set @tableName = 'authors'


Drop Table #TempTable
Create Table #TempTable (
ColumnName varchar(1000) not null,
MaxVal varchar(1000) null,
NullCnt bigint null,
DistinctCnt bigint null
)

Set @position = 1

While exists(Select * From pubs.Information_Schema.Columns Where Table_Name = @tableName and Ordinal_Position = @position)
Begin
Set @sqlStr = null
Select @sqlStr =
'Select ' +
'ColumnName = ''' + COLUMN_NAME + ''', ' +
'MaxVal = convert(varchar,max(' + COLUMN_NAME + ')), ' +
'NullCnt = sum(case when ' + COLUMN_NAME + ' is null then 1 else 0 end), ' +
'DistinctCnt = count(distinct ' + COLUMN_NAME + ') ' +
'From pubs.dbo.' + @tableName + ' '
From pubs.Information_Schema.Columns
Where Table_Name = @tableName
and Ordinal_Position = @position
and Data_Type <> 'bit'

if (@sqlStr is not null)
Begin
Insert Into #TempTable
exec (@sqlStr)
End

Select @Position = @Position + 1
End

Select * From #TempTable


Corey

Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..."
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2006-12-20 : 18:11:44
read this before using dynamic sql for anything:

http://www.sommarskog.se/dynamic_sql.html


www.elsasoft.org
Go to Top of Page

ttoker
Starting Member

2 Posts

Posted - 2006-12-20 : 18:18:21
Well I basically need to report on each column how many null values - max - min values - distinct records etc etc.

If there is a table in SQL Server that gives me this info then I don't need to run any dynamic SQL I guess. Maybe I have to gather stats on all columns and query some system tables? Any ideas on how to do this?

Maybe my initial question was not the right one to start with..
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2006-12-20 : 21:04:53
did you try my query? It does pretty much what you want, except that max and min is not a valid function on some field types...

As far as existing stats... I don't know of them.

Corey

Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..."
Go to Top of Page
   

- Advertisement -