/* Procedure copyright(c) 1995 by Edward M Barlow */

/******************************************************************************
**
** Name        : sp__collist.sp
**
** Summary		: list distinct columns in current databases
**
******************************************************************************/

:r database
go
:r dumpdb
go

IF EXISTS (SELECT * FROM sysobjects
           WHERE  name = "sp__collist"
           AND    type = "P")
   DROP PROC sp__collist

go

CREATE PROC sp__collist( @objname char(32) = NULL, @show_type char(1)=' ' )
/* if @show_type = 'S' will show system tables */
AS 

set nocount on

if not exists (select * from sysobjects where name=@objname and type='U')
	select @objname="%"+@objname+"%"

select distinct
		 column= substring(c.name, 1, 20),
		 tname=  substring(o.name,1,20),
		 type =substring(t.name,1,15),
		 length=c.length,
		 c.status,
		 Nulls="not null",
		 Ident = "identity",
		 prec=0,
		 scale=0
into   #collist
from   syscolumns c, systypes t, sysobjects o
where  c.id = o.id         /* key */
and    o.name like @objname
and    c.number = 0
and    c.usertype = t.usertype
and    o.type in ('U', @show_type )

update #collist
set type=type+'('+rtrim(convert(char(3),length))+')'
where type='varchar'
or    type='char'

update #collist
set type=type+'('+rtrim(convert(char(3),prec))+')'
where type='decimal'

update #collist
set type=type+'('+rtrim(convert(char(3),prec))+')'
where type='numeric'
and   scale=0

update #collist
set type=type
	+'('
	+rtrim(convert(char(3),prec))
	+','
	+rtrim(convert(char(3),scale))
	+')'
where type='numeric'
and   scale>0

update #collist
set  Nulls='null'
where status & 8 != 0

update #collist
set  Ident=''
/* where status & 0x80 = 0 */

print "Listing Of User Columns In Database"
select distinct 
		column,
		type,
		Nulls,
		Ident,
		"Num Tables"="/* "+convert(char(3),count(*))+" Tables */"
from #collist
group by column,type,Nulls
order by column,type

return

if exists ( select *
	from #collist t1,#collist t2
	where t1.type<t2.type
	and   t1.column=t2.column)
begin
		print ""
		print "Listing of Columns with Conflicting Definitions"
		select "Column"=t1.column,
				  "Defn 1"=convert(char(28),t1.tname+'='+t1.type),
				  "Defn 2"=convert(char(28),t2.tname+'='+t2.type)
		from #collist t1,#collist t2
		where t1.type<t2.type
		and   t1.column=t2.column
		order by t1.column,t1.type
end
else
if not exists (select * from sysobjects where name=@objname and type='U')
	begin
	print ""
	print "No Columns with Conflicting Definitions"
	end

if exists (
		  select *
		  from #collist t1,#collist t2
		  where t1.type=t2.type 
		  and   t1.column=t2.column
		  and   t1.Nulls!=t2.Nulls
		  and	  t1.Nulls="null"
)
begin
		  print ""
		  print "Listing of Columns with Conflicting Allow Nulls"
		  select "Column"=t1.column,
				  "Defn 1"=convert(char(28),t1.tname+'='+t1.Nulls),
				  "Defn 2"=convert(char(28),t2.tname+'='+t2.Nulls)
		  from #collist t1,#collist t2
		  where t1.type=t2.type 
		  and   t1.column=t2.column
		  and   t1.Nulls!=t2.Nulls
		  and	  t1.Nulls="null"
		  order by t1.column,t1.type
end
else
if not exists (select * from sysobjects where name=@objname and type='U')
		  begin
		  print ""
		  print "No Columns with Conflicting Allow Nulls"
		  end

drop table #collist
go

GRANT EXECUTE ON sp__collist  TO public
go
