all groups > sql server programming > july 2004 >
You're in the

sql server programming

group:

Looking for data dictionary/definition integrity checking utility



Looking for data dictionary/definition integrity checking utility Nitin Rana
7/12/2004 2:29:30 PM
sql server programming: This script searches for a value in the database. not a
column name but the data. For example you wanna know where
in the database an ID value of 2001 is used. This proc
will list all the tables and column names with the data
included. I am not sure if this script is of any help to
you or not.

CREATE PROCEDURE spSearchOnAllDB @phrase varchar(8000),
@OutFullRecords bit=0 AS

/*
To apply so:
exec spSearchOnAllDB 'Sugar%'
exec spSearchOnAllDB '%soft%'
exec spSearchOnAllDB '_5234_57%', 1
exec spSearchOnAllDB M_cro_oft
*/

declare @sql varchar(8000)
declare @tbl varchar(128)
declare @col varchar(128)
declare @id_present bit

declare @is_char_phrase bit
declare @min_len int
declare @loop_idx int
declare @loop_chr char(1)

set nocount on

if IsNull(@phrase,'')=''
begin
raiserror('Phrase is absent',16,-1)
return
end

select @loop_idx=1, @is_char_phrase=0, @min_len=0

while @loop_idx<=LEN(@phrase)
begin
set @loop_chr=SUBSTRING(@phrase,@loop_idx,1)
if @loop_chr not in ('%','_') set @min_len=@min_len+1
if @is_char_phrase=0 and @loop_chr not in
('%','_','0','1','2','3','4','5','6','7','8','9','.')
set @is_char_phrase=1
set @loop_idx=@loop_idx+1
end

create table #tbl_res
(TableName varchar(128) not NULL,
ColumnName varchar(128) not NULL,
Id int NULL,
ColumnValue varchar(7500) not NULL)

declare CRR cursor local fast_forward for
select t.name, c.name, 1
from sysobjects t, syscolumns c
where t.type='U'
and c.id=t.id
and c.status&0x80=0 -- Not IDENTITY
and exists (select * from syscolumns c2 where t.id=c2.id
and c2.status&0x80=0x80 and c2.xtype in (48,52,56))
and ( (@is_char_phrase=1 and c.xtype in
(175,239,99,231,35,167) and c.length>=@min_len) -- char
only
or (@is_char_phrase=0 and c.xtype not in
(34,165,173,189,61,58,36))) -- char and numeric
union select t.name, c.name, 0
from sysobjects t, syscolumns c
where t.type='U'
and c.id=t.id
and not exists (select * from syscolumns c2 where
t.id=c2.id and c2.status&0x80=0x80 and c2.xtype in
(48,52,56))
and ( (@is_char_phrase=1 and c.xtype in
(175,239,99,231,35,167) and c.length>=@min_len) -- char
only
or (@is_char_phrase=0 and c.xtype not in
(34,165,173,189,61,58,36))) -- char and numeric
order by 1,2
open CRR
fetch CRR into @tbl, @col, @id_present
while @@FETCH_STATUS=0
begin
if @OutFullRecords=0
begin
set @sql='insert into #tbl_res
(TableName,ColumnName,Id,ColumnValue) '
+'select '+char(39)+@tbl+char(39)+', '
+char(39)+@col+char(39)+', '
if @id_present=1 set @sql=@sql+'IDENTITYCOL, '
else set @sql=@sql+'NULL, '
set @sql=@sql+'convert(varchar(7500),'+@col+') '
+'from '+@tbl+' (nolock) '
+'where convert(varchar(8000),'+@col+')
like '+char(39)+@phrase+char(39)
end
if @OutFullRecords=1
begin
set @sql='if exists (select * from '+@tbl+' (nolock) '
+'where convert(varchar
(8000),'+@col+') like '+char(39)+@phrase+char(39)+') '
+'select '+char(39)+@tbl+char(39)+'
TableName, '+char(39)+@col+char(39)+' ColumnName, * '
+'from '+@tbl+' (nolock) where convert(varchar
(8000),'+@col+') like '+char(39)+@phrase+char(39)
end
exec(@sql)
fetch CRR into @tbl, @col, @id_present
end
close CRR
deallocate CRR


if @OutFullRecords=0
begin
-- For the clients supporting new types:
--exec('select * from #tbl_res order by 1,2,3')

-- For the clients who are not supporting new types:
exec('select TableName, ColumnName, Id, convert(varchar
(255),ColumnValue) ColumnValue from #tbl_res order by
1,2,3')
end

drop table #tbl_res



[quoted text, click to view]
Looking for data dictionary/definition integrity checking utility Joergen Bech <jbech<NOSPAM> NO[at]SPAM
7/12/2004 11:12:55 PM

Anyone who can tell me if there is such a utility for SQL Server 2000,
free or commercial?

Basically, what I need is: A tool that will list all definitions in a
database, i.e. all table names, field names, etc. Then, when selecting
a name (e.g. "CustomerCode") tell me where such an identifier is
used throughout the database, i.e. as a table name, a field name, a
parameter in a stored procedure, etc etc, including the definition of
the identifier in each location.

Main requirement is to be enable me to quickly spot data definition
inconsistencies in databases that have - to put it mildly - been
allowed to live for too long without a data dictionary (or developers
ignoring such a dictionary).

Would be nice if it could generate a full data dictionary as well.

Does such a thing exist?

TIA,

Joergen Bech


Re: Looking for data dictionary/definition integrity checking utility Joergen Bech <jbech<NOSPAM> NO[at]SPAM
7/13/2004 4:06:12 AM

That script could come in handy in some situations. Thanks.

But no, it does not address the issue, which is to find
inconsistencies in the data definitions.

Example: If a field is defined
as varchar(100) in one table but only as varchar(50) in another
(or in a stored procedure using the field).

Another example: When a stored procedure uses a field but
the programmer could not be bothered to check the size of it,
so temporary variables or temp table fields are consistently defined
as varchar(2000) - just to be on the safe side.

Stuff that sometimes makes it difficult to maintain old code.

A quick Google search turned up this blog entry:
http://blogs.xtras.net/mikes/PermaLink,guid,8972547e-5371-4bc7-91d4-220b241d5930.aspx
which talks about tools that sound like what I could be looking for:
---snip---
"I have seen tools that generally hunt down all the references to
tables, views, stored procedures, fields, etc. within SQL Server
objects and update them for you. Some tools even look outside in
VB.NET or C# code. But those techniques are, by their very nature,
incomplete."
---snip---

Any ideas what tools he could be talking about?

Regards,

Joergen Bech



On Mon, 12 Jul 2004 14:29:30 -0700, "Nitin Rana" <Nitin_Rana@ibi.com>
[quoted text, click to view]

<---script--->

[quoted text, click to view]
Re: Looking for data dictionary/definition integrity checking utility Bradley M. Small
7/13/2004 9:46:00 AM
Obviously this doesn't solve the problem as asked. But have you considered
using user defined types, and systematically replacing all data definitions
with them?

Just a thought, I have not actually done this anywhere, but have seen many
examples that use this instead of things like varchar(47). Look at most of
the sp_* procedures that access system tables and you will see them used.

Something as simple as "select * from information_schema.columns" will
certainly give you all the information you seek. Then it is merely a matter
of querying it to get what you want, like
"SELECT TABLE_NAME, data_type, CHARACTER_MAXIMUM_LENGTH from
information_schema.columns where COLUMN_NAME = 'MyField'"

Not sure if this helps, good luck.

-- B

[quoted text, click to view]

Re: Looking for data dictionary/definition integrity checking utility Joergen Bech <jbech<NOSPAM> NO[at]SPAM
7/13/2004 8:27:05 PM

Thanks. I usually work at the front (user) end (VB6), leaving the
intricacies of SQL Server to others, so I was not aware of all the
information to be found in the information_schema.* views.

This looks useful. I noticed a product named "SQL Server Find 3.1",
which is sorta what I'm looking for, but the tool is far too primitive
to be of any use to me (my apologies to the author). Before I came
across it I had already drawn up a long (albeit unsorted) list of
requirements, but ...

I guess I'll have to decide if I should spend a great deal of time
writing a tool that will save me the same amount of time :) - hoping
that I'll need it more than once.

Regards,

JB



On Tue, 13 Jul 2004 09:46:00 -0400, "Bradley M. Small"
[quoted text, click to view]
AddThis Social Bookmark Button