Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need full data information of Sys.Schemas,Sys.Tables,Sys.Columns with joining Information_Schema_columns

What I have tried:

select d.name as Schema_Name ,db_name()as Database_Name ,
b.name as Table_Name,c.name as Column_Name ,
isc.ORDINAL_POSITION,isc.COLUMN_DEFAULT,isc.DATA_TYPE, isc.CHARACTER_MAXIMUM_LENGTH,
isc.NUMERIC_PRECISION, isc.NUMERIC_PRECISION_RADIX, isc.NUMERIC_SCALE,isc.DATETIME_PRECISION
from
sys.schemas as d
inner join
sys.tables as b
on d.schema_id=b.schema_id
inner join
sys.columns as c
on c.object_id=b.object_id
inner join INFORMATION_SCHEMA.COLUMNS isc on
isc.TABLE_NAME = OBJECT_NAME(c.object_id) and isc.COLUMN_NAME = c.name and isc.DATA_TYPE <> 'bit'
and isc.COLUMN_NAME like '%email%'
order by Database_Name,Schema_Name,Table_Name,Column_Name
Posted
Updated 27-Apr-21 6:03am
Comments
Wendelius 26-Apr-21 15:53pm    
What is the problem you're having?
Dinesh Bhati 2021 27-Apr-21 1:16am    
I dont have any problem. I want to Optimize it. So that i can get better results and query exwcutes fatser
RedDk 27-Apr-21 12:19pm    
Weel, then to optimize I'd start by running a query and posting some output showing ME the output for this [Column Details].
RedDk 26-Apr-21 17:23pm    
Just to confirm, try commenting out "and isc.COLUMN_NAME like '%email%'" (line 15) to see that you can return something. Hopefully you'll see a big table full of information about the instance you're running it on.

If there's no such COLUMN_NAME where the word "email" is surreptitiously contained, leaving that line in will return no records.
Dinesh Bhati 2021 27-Apr-21 1:08am    
Actually I need Column Details which have Email information

So if the question is about optimizing, there's mot too much you can do since these are system views you're querying.

However, from performance point of view but also to achieve simple query structure it would be best to stick solely with INFORMATION_SCHEMA views and to use all joining columns. Have a try with the following example. It should perform quite well.

SQL
SELECT s.CATALOG_NAME      AS Database_Name,
       s.SCHEMA_NAME       AS Schema_Name,
       st.TABLE_NAME       AS Table_Name,
	   sc.COLUMN_NAME      AS Column_Name,
       sc.ORDINAL_POSITION,
	   sc.COLUMN_DEFAULT,
	   sc.DATA_TYPE,
	   sc.CHARACTER_MAXIMUM_LENGTH,
       sc.NUMERIC_PRECISION, 
	   sc.NUMERIC_PRECISION_RADIX, 
	   sc.NUMERIC_SCALE,
	   sc.DATETIME_PRECISION
FROM       INFORMATION_SCHEMA.SCHEMATA s
INNER JOIN INFORMATION_SCHEMA.TABLES   st ON  st.TABLE_SCHEMA  = s.SCHEMA_NAME
                                          AND st.TABLE_CATALOG = s.CATALOG_NAME
INNER JOIN INFORMATION_SCHEMA.COLUMNS  sc ON  sc.TABLE_SCHEMA  = st.TABLE_SCHEMA
                                          AND sc.TABLE_NAME    = st.TABLE_NAME
										  AND sc.TABLE_CATALOG = st.TABLE_CATALOG
WHERE sc.DATA_TYPE   <> 'bit'
AND   LOWER(sc.COLUMN_NAME) LIKE '%email%'
ORDER BY s.CATALOG_NAME,
         s.SCHEMA_NAME,
         st.TABLE_NAME,
	     sc.COLUMN_NAME;
 
Share this answer
 
Comments
Dinesh Bhati 2021 27-Apr-21 12:22pm    
Dear Wendelius Thanks Works good for me
Wendelius 27-Apr-21 18:42pm    
You're welcome :)
If I remember correctly I did something like that in a converter project, see: Convert SQL Server Database to PostgreSQL[^]
It is rather old though and was written for SQL Server 2008.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900