I'm writing a application using a database backend, but I need to find out all the column names and the table they belong to via SQL. I know you can query sys.columns
, sys.tables
, and sys.databases
, but what I want to do is join them all to get something like the below result (basically displaying all columns and tables from the DB):
+-----------+------------+
| Tablename | columnname |
+-----------+------------+
| Table1 | Id |
| Table1 | Name |
| Table1 | Owner |
| Table2 | Id |
| Table2 | Name |
| Table2 | True |
+-----------+------------+
I've had a good look in the sys schema, but I can't find any parent reference in sys.columns
or sys.tables
.
INFORMATION_SCHEMA
is slow when you have lots of tables (query like this take about 30s on my database where i have ~1800 tables - more then half of them created by bug, thats why we noticed it - query to sys.columns is almost instant). Yes, i know, this is not realy the "generally" situation, just FYI :) – Macey