Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SQL Server schema via a SQL query?

I've inherited a clunky and horribly un-documented site from a bad developer and am trying to get a look at the database schema. Unfortunately the web host is the worst I've ever dealt with and has no control panel capability for viewing the db schema or even exporting tables.

Is there any way that I can get a look at the schema via a SQL query (this would be with ASP + SQL Server)? My end goal here is to see what tables exist, possibly get a SQL dump of the vital tables, and then recreate the whole thing the right way.

like image 882
Jeff Avatar asked Aug 23 '09 00:08

Jeff


People also ask

How do I find schema in SQL query?

You can get a list of the schemas using an SSMS or T-SQL query. To do this in SSMS, you would connect to the SQL instance, expand the SQL database and view the schemas under the security folder. Alternatively, you could use the sys. schemas to get a list of database schemas and their respective owners.

How can I get database schema in SQL Server?

Right-click the Security folder, point to New, and select Schema. In the Schema - New dialog box, on the General page, enter a name for the new schema in the Schema name box. In the Schema owner box, enter the name of a database user or role to own the schema.

How can you see the schema of the table?

To show the schema, we can use the DESC command. This gives the description about the table structure.


2 Answers

The INFORMATION_SCHEMA schema is a good place to start:

SELECT * FROM INFORMATION_SCHEMA.TABLES
SELECT * FROM INFORMATION_SCHEMA.VIEWS

...and so on.

You might also want to have a look at using SMO, an API to get at the metadata in SQL Server.

like image 195
Jeremy Smyth Avatar answered Nov 02 '22 17:11

Jeremy Smyth


I'm not sure if simple queries like

SHOW TABLES;
DESCRIBE table_name;
SHOW TABLE STATUS from table_name;

are valid in MS SQL. They would also be useful

like image 33
pavium Avatar answered Nov 02 '22 17:11

pavium