DECLARE @query VARCHAR(8000),
@table_name VARCHAR(100),
@column_name VARCHAR(100),
@datatype VARCHAR(20)
WHILE EXISTS ( SELECT 1
FROM [index]
WHERE value_type IS NULL )
BEGIN
SELECT TOP 1
@table_name = table_name,
@column_name = column_name,
@datatype = datatype
FROM [index]
WHERE value_type IS NULL
IF @datatype NOT IN ( 'uniqueidentifier','int','smallint','bigint','numeric','float','money',
'char','nchar','varchar','nvarchar','text','ntext','bit',
'datetime','smalldatetime' )
BEGIN
UPDATE [index]
SET value_type = 'n/a'
WHERE table_name = @table_name
AND column_name = @column_name
END
ELSE
BEGIN
IF @datatype = 'uniqueidentifier'
BEGIN
SET @query = '
DECLARE @value_type VARCHAR(30),
@value_count INT,
@value_unique INT,
@value_null INT
SET @value_count = ( SELECT count(1)
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
SET @value_unique = ( SELECT count(DISTINCT [' + @column_name + '])
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
SET @value_null = ( SELECT count(1)
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NULL )
SET @value_type = ( CASE
WHEN @value_count = @value_unique
AND @value_null = 0
THEN ''UID, unique, not null''
WHEN @value_count = @value_unique
AND @value_null > 0
THEN ''UID, unique, null''
WHEN @value_count > @value_unique
AND @value_null = 0
THEN ''UID, not null''
WHEN @value_count > @value_unique
AND @value_null > 0
THEN ''UID, null''
END )
UPDATE [index]
SET value_type = @value_type,
value_count = @value_count,
value_unique = @value_unique,
value_null = @value_null
WHERE table_name = ''' + @table_name + '''
AND column_name = ''' + @column_name + '''
'
EXEC(@query)
END
IF @datatype IN ( 'int','smallint','bigint','numeric','float','money' )
BEGIN
SET @query = '
DECLARE @value_type VARCHAR(30),
@value_min VARCHAR(20),
@value_avg VARCHAR(20),
@value_max VARCHAR(20),
@value_count INT,
@value_unique INT,
@value_null INT,
@value_list VARCHAR(255)
SET @value_min = ( SELECT min([' + @column_name + '])
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
SET @value_avg = ( CASE
WHEN ''' + @datatype + ''' IN ( ''int'',''smallint'' )
THEN ( SELECT avg(cast([' + @column_name + '] AS BIGINT))
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
ELSE ( SELECT avg([' + @column_name + '])
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
END )
SET @value_max = ( SELECT max([' + @column_name + '])
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
SET @value_count = ( SELECT count(1)
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
SET @value_unique = ( SELECT count(DISTINCT [' + @column_name + '])
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL )
SET @value_null = ( SELECT count(1)
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NULL )
IF EXISTS ( SELECT 1
FROM [' + @table_name + ']
WHERE cast([' + @column_name + '] AS VARCHAR(20)) LIKE ''%.%''
OR cast([' + @column_name + '] AS VARCHAR(20)) LIKE ''%,%'' )
BEGIN
SET @value_type = ''number''
END
ELSE
BEGIN
SET @value_type = ''integer''
END
IF @value_unique BETWEEN 1 AND 10
BEGIN
DECLARE @counter SMALLINT
SET @counter = @value_unique
WHILE @counter > 0
BEGIN
SET @value_list = ( SELECT isnull(@value_list,'''') + cast([' + @column_name + '] AS VARCHAR(20)) + ''; ''
FROM ( SELECT TOP (@counter) [' + @column_name + '],
ROW_NUMBER() OVER (ORDER BY [' + @column_name + '] DESC) AS zeilennr
FROM ( SELECT DISTINCT [' + @column_name + ']
FROM [' + @table_name + ']
WHERE [' + @column_name + '] IS NOT NULL ) tt
ORDER BY [' + @column_name + '] DESC) t
WHERE t.zeilennr = @counter )
SET @counter = @counter - 1
END
END
UPDATE [index]
SET value_type = @value_type,
value_min = @value_min,
value_avg = @value_avg,
value_max = @value_max,
value_count = @value_count,
value_unique = @value_unique,
value_null = @value_null,
value_list = @value_list
WHERE table_name = ''' + @table_name + '''
AND column_name = ''' + @column_name + '''
'
EXEC(@query)
END