I am creating a query that returns the number of columns in each table, but I want to exclude Views.
The following works but returns View results:
SELECT COUNT(*), table_name
FROM INFORMATION_SCHEMA.COLUMNS
Group By table_name
Any suggestions?
NOTE: MSSQL 2005+
mysql> SELECT COUNT(*) AS NUMBEROFCOLUMNS FROM INFORMATION_SCHEMA. COLUMNS -> WHERE table_schema = 'business' AND table_name = 'NumberOfColumns'; The output displays the number of columns.
USE db_name; DESCRIBE table_name; it'll give you column names with the type.
This assumes SQL 2005 or higher
SELECT
t.name,
count(c.name)
FROM
sys.tables t
inner join sys.columns c
ON t.object_id = c.object_id
group by t.name
Something like this:
SELECT COUNT(col.column_name), col.table_name
FROM information_schema.columns col
JOIN information_schema.tables tbl
ON tbl.table_name = col.table_name
AND tbl.table_schema = col.table_schema
AND tbl.table_catalog = col.table_catalog
AND tbl.table_type <> 'VIEW'
GROUP BY col.table_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With