Get SQL Server schema via a SQL query? - sql-server

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.

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.

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

SchemaSpy http://schemaspy.sourceforge.net/ is a great tool for analyzing existing databases. It generates html lists of table and constraints as well as a graphical representation of relationships

Related

How/what to import to dbdiagram.io?

Hey I'm pretty new to databases and I want to create a diagram of the db I'm using with dbdiagram.io so that I can't understand the relationships better. What sort of script/file am I supposed to use to import the schema from sql server? I'm currently using azure data studio to query the database. I would prefer not to have to write out all the tables by hand.
Based on the docs and the DBML language, you need to generate a DBML script in the following format from your RDBMS of choice - in this case, SQL Server:
Table Users {
UserID integer [pk]
}
Table Posts {
PostID integer [pk]
UserID integer [ref: > Users.UserID] // many-to-one
}
Note that this DBML output is not intended to run on your RDBMS, SQL Server or otherwise - those platforms don't understand DBML. It is an example of output you need to generate from your RDBMS that you can paste directly into the left editor pane on dbdiagram.io, as demonstrated here.
This more complicated example - which uses DBML to build a diagram from SQL Server based on a subset of the tables in Stack Exchange Data Explorer (and which I described here) - took about 40 seconds to create.
You can build this syntax from a set of tables without having to hard-code each one using catalog views like sys.tables and sys.columns, but there are a lot of little things that you'll have to do to make it compatible with this web site, and that will be a project all its own. Basically, nothing in SQL Server exists to generate nice, tidy DBML for you.
If you don't want to use DBML but would rather use the Import feature on the web site, then you can generate traditional DDL scripts instead - from SQL Server, you can do this by right-clicking the database in SSMS / Object Explorer and using Script Database as > CREATE To. Azure Data Studio doesn't yet have this functionality, but you can use an extension.

Is there a query to see all the references in a SQL Server table?

I want a simple way to find all the Foreign Keys of my table.
For example having a table Customers, I want to find the tables with a relationship to this table, I use to find relationships using the Diagram but it is too slow.
Desired result = Customers_Accounts, Customers_Cities, Customers_Properites, etc
Yes just select your table and press ALT + F1, at the bottom of the result set you will see all the references
The reason that the dependencies is slow is due to the number and complexity of queries that sql sever runs in order to show you a nice hierarchical structure.
There are some ways to get the data you need which vary depending on the version of sql and how much info you are looking for.
Taken from Microsoft Docs - View the Dependencies of a Table
USE AdventureWorks2012;
GO
SELECT * FROM sys.sql_expression_dependencies
WHERE referencing_id = OBJECT_ID(N'Production.vProductAndDescription');
GO
Obviously you'll need to change the AdventureWorks2012 to your database and Production.vProductAndDescription to your Customer schema and table name.
The above works for the latest sql server editions, for older editions you may need to refer to the following links:
MSSQL Tips - Different Ways to Find SQL Server Object Dependencies
MSSQL Tips - Listing SQL Server Object Dependencies

How to add the diagram code to the table name in the SQL generation in Powerdesigner?

I have limited experience hacking PowerDesigner so I don't even know if what I need would be possible.
Context: I'm doing a Proof of Concept of HP Vertica data modeling. Since PowerDesigner does not have any xdb for Vertica I'm doing the PoC as ANSI SQL.
Problem: In ANSI SQL the SQL generated is neutral and compatible with Vertica, but I cannot handle DB users (read owners) so there is no information related to Schema or table owner when the SQL is generated. I would need something like
CREATE TABLE owner_name.table_name
instead of what I get now
CREATE TABLE table_name
Despite not being able to use table owners, I'm grouping them in different diagrams, so I could solve the "problem" if I could generate in the SQL code something like
CREATE TABLE diagram_name.table_name
since the outcome in the SQL code would be the same even if I'm not using strictly the DB users it would work for me perfectly. What I don't know is if this is even possible in PowerDesigner, and if so, where to tune o hack to achieve this.
Any hint?
Thank you very much in advance.

Best way to perform distributed SQL query and joins, calling from .Net code

Here's my scenario:
I have to query two PeopleSoft Databases on different servers (both are SQL Server 2000) and do a join of the data. My application is a .Net application (BizTalk).
I'm wondering what the best option is with regards to performance?
use standard select queries to get data
and do the join in memory (e.g. LINQ) for example
generated complex dynamic queries using LINKED Server, e.g.
select blah
from Server1.HRDB.dbo.MyTable1
left join Server2.FinanceDb.dbo.MyTable2
use standard select queries to get the data into an intermediate / staging sql server database and do my queries / joins on this database instead.
should I consider using SSIS? ( are there features here that might be better than doing an in-memory, e.g. LINQ? )
I wish I could use stored procedures on the source database, but the owners of the PeopleSoft database refuse it
The main constraints we have is that the source database is old (SQL Server 2000) and that performance of the source database is paramount. Whatever queries I run on this server must not block the other users. Hence, the DBAs are adamant about no Stored Procedures. They also believe that queries involving Linked Servers will trump (i.e. take higher priority) to other queries being run against the the database.
Any feedback would be greatly appreciated.
Thanks!
Update: additional background information on the project
We are primarily integrating PeopleSoft databases (the HR and Finance) into another product. Some are simple - like AccountCode and Department. Others are more complex, like the personal data, job, and leave accrual. Some are real-time, other's are scheduled, and other's are 'batch' (e.g. at payroll runs).
Regardless, we have to get source data out of PeopleSoft database -- and my hope had been to let the (source) database do the 'heavy' lifting by executing SQL Queries. I don't really want BizTalk, or SSIS, or C# LINQ to be the ones doing the transformations/filtering.
Definitely open to suggestions.

SQL Command for generating schema text (similar to CreateTo or AlterTo)

SQL Server 2005. Is there a sql query that will return a text field containing the same type of schema info as you would find in doing a right click table -> Script Table As -> Create To (or Alter To) from SQL Server Management Studio ?
I'm looking for a single/flat format that describes the entire table, including constraints, indices, etc.
I am aware of:
sp_help table_name
but that doesn't provide the single flat format I'm looking for. Ideally it would be in a scriptable format, such as the AlterTo result that could be executed against the server.
This is for a scheduled process that documents table schemas on a nightly basis for checking in to version control (SVN).
Not really. A table def is a collection of columns, constraints etc.
There is an SVN plugin that may help called ScriptDB4SVN. I've not used it personally, I'm going on hearsay.
Was searching the 'net again for an answer to this, and came across this SO question. It doesn't accurately capture all the same data as SQL Management Studios Create-to, but enough for my purposes (scripting the database structure for version control purposes).
There is no such command in SQL Server. This is primarily because the Scripting facilitiy is actually in SMO and not in SQL Server itself. There are a number of free console command-line tools that can do it that you could call via xp_CmdShell.
However, if you really want to do this from T-SQL, then you will need a script or stored procedure that enumerates all of the tables attributes, columns, column datatypes, defaults, nullabilty, etc. etc. and then reassembles it into a CREATE TABLE script. This is a Huge task. That's the bad news. The good news is that someone (Lowell Izaguirre) has already done this and posted it in this article (http://www.sqlservercentral.com/scripts/Miscellaneous/30730/) at SQLServerCentral.Com.
Enjoy.
Not really - you can either use C# (or VB.NET) and SMO (SQL Management Objects) to script out your database objects (tables and all), or you can use SQL to get the list of columns for a table:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Your Table Name here'
But I don't know of any easy way in SQL itself to create Create/Alter scripts for database objects, sorry.
Marc

Resources