Creating a Data Dictionary with example data (SQL) - sql

I am looking to run a script on a SQL database to create a Data Dictionary with an example of the data for each field.
To keep it simple I would just like to include the data from the first row of each table along with each table name and column name
So something like this:
Table Name
Field Name
Example Data
Customer
ID
CU1
Customer
Title
Mrs
Customer
Name
Anne
Customer
Order No
ORD1
etc.
Is there an easy way to do this with a SQL script?

Somebody smarter than me could probably optimize this and remove the cursor but the Dynamic SQL was giving me a headache. I think in this scenario, a cursor is acceptable
DROP TABLE IF EXISTS ##DataDictionary
CREATE TABLE ##DataDictionary (TableName SYSNAME, ColumnName SYSNAME, SampleData NVARCHAR(MAX))
DECLARE #TableName SYSNAME
DECLARE #ColumnName SYSNAME
DECLARE #SQL NVARCHAR(MAX)
DECLARE cur CURSOR FOR
SELECT t.name AS TableName,c.Name AS ColumnName
FROM sys.tables t
JOIN sys.columns c ON t.object_id = c.object_id
OPEN cur
FETCH cur INTO #TableName,#ColumnName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #SQL = ''
SELECT #SQL = '
INSERT INTO ##DataDictionary(TableName,ColumnName,SampleData)
SELECT '''+#TableName+''','''+#ColumnName+'''
,(SELECT TOP 1 '+QUOTENAME(#ColumnName)+' FROM '+QUOTENAME(#TableName)+' ORDER BY NEWID()) -- NewID randomly selects a sample row
'
print #SQL
EXEC (#SQL)
FETCH cur INTO #TableName,#ColumnName
END
CLOSE cur
DEALLOCATE cur
SELECT * from ##DataDictionary

Related

How can i select combine data of multiple table from the following sql query

Actually it is dynamic query and i want the data of the tables who has the 'Schema: Document' .
select Id,Code,[Address]
from sys.tables
where name in (select '[Document].'+name as table_name
from sys.tables
where schema_name(schema_id) = 'Document')
I assume, you have some tables, each of which has
the schema as Document
the 3 fields Id,Code,[Address]
I'm using a cursor.
Also note that this doesn't cater to any table which doesn't have the relevant fields, but has the same Schema. Also assuming that you are OK with data as query results
Declare #SQLQuery varchar(max)
DECLARE #returnData TABLE (tblName varchar(100), Id int,Code varchar(100),[Address] varchar(100))
DECLARE csr_docTbl CURSOR
FOR select ' Select ''' + name + ''' as Tbl, Id,Code,[Address] from [Document].' + name as table_name
from sys.tables
where schema_name(schema_id) = 'Document'
OPEN csr_docTbl;
FETCH NEXT FROM csr_docTbl INTO #SQLQuery
WHILE ##FETCH_STATUS = 0
Insert into #returnData(tblName, Id, Code, [Address])
Exec (#SQLQuery)
FETCH NEXT FROM csr_docTbl INTO #SQLQuery
END;
CLOSE csr_docTbl;
DEALLOCATE csr_docTbl;
Select * from #returnData

Find all database tables where a common column is set to a value

All database tables have a UserId field of [uniqueidentifier] type.
I need to query the entire database and get the list of tables that have UserId set to a specific value.
Right now I achieved this by using cursor and the results are horrible and are difficult to read. How can I improve this query to retrieve back a clear list with tables and count of record that have UserId set to a specific value, instead of using this:
DECLARE #TableName VARCHAR(127);
DECLARE #Value VARCHAR(512);
DECLARE #SqlCommand varchar(1000)
--Use cursor to loop through database tables that contain UserId column
DECLARE db_cursor CURSOR FOR
SELECT t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name = 'UserId';
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO #TableName;
WHILE ##FETCH_STATUS = 0
BEGIN
--Check if the next table has any UserId matching the where clause
EXEC('SELECT COUNT(UserId) , ''' + #TableName + ''' FROM ' + #TableName + ' WHERE UserId = ''FF13ACCA-022C-4296-AB3D-A35700E35BB3''');
FETCH NEXT FROM db_cursor INTO #TableName;
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
You made all the difficult part, just put the value in a temp table and select them once you've finished.
DECLARE #TableName VARCHAR(127);
DECLARE #Value VARCHAR(512);
DECLARE #SqlCommand varchar(1000)
--Creta temp table
CREATE TABLE #Results (Number int, Tablename sysname)
--Use cursor to loop through database tables that contain UserId column
DECLARE db_cursor CURSOR FOR
SELECT t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name = 'UserId';
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO #TableName;
WHILE ##FETCH_STATUS = 0
BEGIN
--Check if the next table has any UserId matching the where clause
EXEC('INSERT INTO #Results (Number, ''' + #TableName + ''') SELECT COUNT(UserId) , ''' + #TableName + ''' FROM ' + #TableName + ' WHERE UserId = ''FF13ACCA-022C-4296-AB3D-A35700E35BB3''');
FETCH NEXT FROM db_cursor INTO #TableName;
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
SELECT * FROM #Results
DROP TABLE #Results
I cannot test it but this should be the way

Looping through a column in SQL table that contains names of other tables

I have fairly new to using SQL, currently I have a table that has a column that contains the names of all the tables I want to use for one query, so what I want to do is to loop through that column and go to every single one of these tables and then search one of their columns for a value (there could be multiple values), so whenever a table contains the value, I will list the name of the table. Could someone give me a hint of how this is done? Is cursor needed for this?
I don't have enough reputation to comment but is the table with the column that contain the table names all in one column, meaning that all the table names are comma separated or marked with some sort of separator? This would cause the query to be a little more complicated as you would have to take care of that before you start looping through your table.
However, this would require a cursor, as well as some dynamic sql.
I will give a basic example of how you can go about this.
declare #value varchar(50)
declare #tableName varchar(50)
declare #sqlstring nvarchar(100)
set #value = 'whateveryouwant'
declare #getTableName = cursor for
select tableName from TablewithTableNames
OPEN #getTableName
fetch NEXT
from #getTableName into #tableName
while ##FETCH_STATUS = 0
BEGIN
set #sqlstring = 'Select Count(*) from ' + #tableName + 'where ColumnNameYouwant = ' + #value
exec #sqlstring
If ##ROWcount > 0
insert into #temptable values (#tableName)
fetch next
from #getTableName into #tableName
END
select * from #temptable
drop table #temptable
close #getTableName
deallocate #getTableName
I'm currently not able to test this out as for time constraint reasons, but this is how I would go about doing this.
You could try something like this:
--Generate dynamic SQL
DECLARE #TablesToSearch TABLE (
TableName VARCHAR(50));
INSERT INTO #TablesToSearch VALUES ('invoiceTbl');
DECLARE #SQL TABLE (
RowNum INT,
SQLText VARCHAR(500));
INSERT INTO
#SQL
SELECT
ROW_NUMBER() OVER (ORDER BY ts.TableName) AS RowNum,
'SELECT * FROM ' + ts.TableName + ' WHERE ' + c.name + ' = 1;'
FROM
#TablesToSearch ts
INNER JOIN sys.tables t ON t.name = ts.TableName
INNER JOIN sys.columns c ON c.object_id = t.object_id;
--Now run the queries
DECLARE #Count INT;
SELECT #Count = COUNT(*) FROM #SQL;
WHILE #Count > 0
BEGIN
DECLARE #RowNum INT;
DECLARE #SQLText VARCHAR(500);
SELECT TOP 1 #RowNum = RowNum, #SQLText = SQLText FROM #SQL;
EXEC (#SQLText);
DELETE FROM #SQL WHERE RowNum = #RowNum;
SELECT #Count = COUNT(*) FROM #SQL;
END;
You would need to change the "1" I am using as an example to the value you are looking for and probably add a CONVERT/ CAST to make sure the column is the right data type?
You actually said that you wanted the name of the table, so you would need to change the SQL to:
'SELECT ''' + ts.TableName + ''' FROM ' + ts.TableName + ' WHERE ' + c.name + ' = 1;'
Another thought, it would probably be best to insert the results from this into a temporary table so you can dump out the results in one go at the end?

Search sql database for a column name, then search for a value within the retuned columns

This query will search a database for a specific column name. I would like to go one step further and search the returned columns for a specific value.
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name,
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%Example%'
Any ideas?
Many thanks
For example, I have a database named Organisation. I have more than one table where tax_id column is present.
Most of the time, we have to find such a column from the whole database.
The solution is provided below:
select table_name,column_name from information_schema.columns
where column_name like '%tax%'
There is no matter in query to database name which ever you just need to change willing Column Name and will found required result
Search any value Like computer in whole database in which column and in which tables value computer exists
For it first we need to write a store procedure then we reuse it for our search i got it from http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm very perfect result.
after executing store procedure we got required result as in given below image.
Image showing complete search result of keyword computer from whole database.
Above was concept to solve it.Exact Query fullfilling above requirment is below
Select tax_id from (select table_name from information_schema.columns
where column_name = 'tax_id') as temp
There is not such system table present for this kind of searching. Whereas you can try this for your purpose
DECLARE #ValueToSearch NVARCHAR(500)
DECLARE #SearchColumn NVARCHAR(100)
DECLARE #TableName NVARCHAR(200)
DECLARE #ColumnName NVARCHAR(200)
SET #ValueToSearch ='YOUR VALUE TP SEARCH'
SET #SearchColumn = 'YOUR COLUMN'
DECLARE #getResult CURSOR
SET #getResult = CURSOR FOR
SELECT t.name AS table_name,c.name AS column_name FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE c.name = #SearchColumn
OPEN #getResult
FETCH NEXT FROM #getResult INTO #TableName,#ColumnName
WHILE ##FETCH_STATUS = 0
BEGIN
SET NOCOUNT ON ;
DECLARE #RESULT INT;
DECLARE #TYPE INT
DECLARE #QUERY NVARCHAR(1000)
SET #QUERY = 'select #RESULT=count(*) from ' + ISNULL(#TableName,'') +' WHERE '+ ISNULL(#ColumnName,'')+'='''+ ISNULL(#ValueToSearch,'') +''''
EXEC sp_executesql #QUERY,
N'#result int OUTPUT, #type int OUTPUT',
#RESULT OUTPUT,
#TYPE OUTPUT
IF(ISNULL(#RESULT,0)>0)
BEGIN
SET NOCOUNT ON;
SELECT ' COLUMN '+ #ColumnName + ' OF TABLE ' +#TableName+ ' HAS THIS VALUE.'
END
FETCH NEXT FROM #getResult INTO #TableName,#ColumnName
END
CLOSE #getResult
DEALLOCATE #getResult
Thanks
Manoj

How to detect and remove a column that contains only null values?

In my table table1 there are 6 columns Locations,a,b,c,d,e.
Locations [a] [b] [c] [d] [e]
[1] 10.00 Null Null 20.00 Null
[2] Null 30.00 Null Null Null
i need the result like
Locations [a] [b] [d]
[1] 10.00 Null 20.00
[2] Null 30.00 Null
My question is how to detect and delete column that contains all null values using sql query.
Is it possible?
If yes then please help and give sample.
Here is a fast (and ugly) stored proc that takes the name of the table and print (or drop if you want it to) the fields that are full of nulls.
ALTER procedure mysp_DropEmptyColumns
#tableName nvarchar(max)
as begin
declare #FieldName nvarchar(max)
declare #SQL nvarchar(max)
declare #CountDef nvarchar(max)
declare #FieldCount int
declare fieldNames cursor local fast_forward for
select c.name
from syscolumns c
inner join sysobjects o on c.id=o.id
where o.xtype='U'
and o.Name=#tableName
open fieldNames
fetch next from fieldNames into #FieldName
while (##fetch_status=0)
begin
set #SQL=N'select #Count=count(*) from "'+#TableName+'" where "'+#FieldName+'" is not null'
SET #CountDef = N'#Count int output';
exec sp_executeSQL #SQL, #CountDef, #Count = #FieldCount output
if (#FieldCount=0)
begin
set #SQL = 'alter table '+#TableName+' drop column '+#FieldName
/* exec sp_executeSQL #SQL */
print #SQL
end
fetch next from fieldNames into #FieldName
end
close fieldNames
end
This uses a cursor, and is a bit slow and convoluted, but I suspect that this is a kind of procedure that you'll be running often
How to detect whether a given column has only the NULL value:
SELECT 1 -- no GROUP BY therefore use a literal
FROM Locations
HAVING COUNT(a) = 0
AND COUNT(*) > 0;
The resultset will either consist of zero rows (column a has a non-NULL value) or one row (column a has only the NULL value). FWIW this code is Standard SQL-92.
SQL is more about working on rows rather than columns.
If you're talking about deleting rows where c is null, use:
delete from table1 where c is null
If you're talking about dropping a column when all rows have null for that column, I would just find a time where you could lock out the DB from users and execute one of:
select c from table1 group by c
select distinct c from table1
select count(c) from table1 where c is not null
Then, if you only get back just NULL (or 0 for that last one), weave your magic (the SQL Server command may be different):
alter table table1 drop column c
Do this for whatever columns you want.
You really need to be careful if you're deleting columns. Even though they may be full of nulls, there may be SQL queries out there that use that column. Dropping the column will break those queries.
SELECT * FROM table1 WHERE c IS NOT NULL -- or also SELECT COUNT(*)
To detect if indeed this column has no values at all.
ALTER TABLE table1 DROP COLUMN c
is the query to remove the column if it is deemed desirable.
Try this stored procedure with your table name as input.
alter proc USP_DropEmptyColumns
#TableName varchar(255)
as
begin
Declare #col varchar(255), #cmd varchar(max)
DECLARE getinfo cursor for
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID
WHERE t.Name = #TableName
OPEN getinfo
FETCH NEXT FROM getinfo into #col
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #cmd = 'IF NOT EXISTS (SELECT top 1 * FROM [' + #TableName + '] WHERE [' + #col + '] IS NOT NULL)
BEGIN
ALTER TABLE [' + #TableName + '] DROP Column [' + #col + ']
end'
EXEC(#cmd)
FETCH NEXT FROM getinfo into #col
END
CLOSE getinfo
DEALLOCATE getinfo
end
PROC PRINT DATA=TABLE1;RUN;
PROC TRANSPOSE DATA=TABLE1 OUT=TRANS1;VAR A B C D E;RUN;
DATA TRANS2;SET TRANS1;IF COL1 = . AND COL2 = . THEN DELETE;RUN;
PROC TRANSPOSE DATA=TRANS2 OUT=TABLE2 (DROP=_NAME_);VAR COL1-COL2;RUN;
PROC PRINT DATA=TABLE2;RUN;
If you want to perform the stored proc on all the tables in your database.
DECLARE #table_name AS VARCHAR(128);
DECLARE table_cursor CURSOR FOR
SELECT name FROM sys.tables;
OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO #table_name;
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC USP_DropEmptyColumns #table_name;
FETCH NEXT FROM table_cursor INTO #table_name;
END;
CLOSE table_cursor;
DEALLOCATE table_cursor;