Checking views and their default dependencies - sql-server

Is it the default for a View to reference base tables in its own database in SQL Server Management Studio if it doesn't explicitly point to a specific database? I have tables with the same names across several databases on the same server and I'm not sure how to check which tables it's using. I used this query to see the Table_Catalog:
SELECT view_name, Table_Name,* FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
However I saw a warning that querying the sys.objects is the only way to really confirm the dependencies but I don't have a good query to see database information.
Appreciate any help. Thank you.

A view same as any query will reference tables from it's own database unless database name is explicitly mentioned with the table name like databaseOne.dbo.TableName

Related

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

Validate that referenced tables exist when creating a procedure

SQL Server only seems to validate that a procedure contains valid T-SQL and that all columns referenced for existing tables are valid. Meaning that I can create a procedure that references non-existent tables.
So, how might I verify that only existent tables are referenced?
Please note that I do not care about dynamic SQL in this case as I believe that would be out of scope.
You are correct that it does NOT validate if the tables, views or other objects exist. This is known as deferred name resolution. https://technet.microsoft.com/en-us/library/ms190686.aspx
The only way I know of to validate the objects actually exist is to execute the procedure.
Yes it will not check, but some of MVP's posted feature for this...
https://connect.microsoft.com/SQLServer/feedback/details/260762/add-optional-checks-for-more-robust-development
You can actually make use of SSDT tool in visual studio so that the once you create a procedure that actually refers a non existent table, then the Visual Studio gives a warning
Deferred name resolution is a SQL Server "feature" in there for convenience as it allows development to be done "out of order". But it does allow invalid objects to slip in there.
Redgate SQL Prompt (commercial tool) has a Find Invalid Objects feature that will list objects that reference objects that don't exist.
It has a 14-day trial so feel free to download it and give it a go. If it doesn't spot the objects you expect it to, do let us know!
Query the database system's "tables" table.
After connecting to the database, use this to view all tables in the database.
SELECT * FROM sys.tables
Use this to view a specific table.
SELECT * FROM sys.tables WHERE name='<YourTableName>'

How to use SSDT to compare database with different default schema

I have two database that belongs to two different SQL Server. Their database schema should be very similar but somehow different application generate different 'default schema' on the tables and views. Now when I am trying to compare the schema by using SSDT, I don't know how to ignore this default schema (I found it has a setting but it didn't value, same result showing as treating them as different set of objects).
e.g. Database A vs Database B
[dbuser].[TableA] vs [dbo].[TableA]
SSDT claims they are different..... :S
Please give me some advice... I expect I don't have to hack the database in order to achieve the comparison....
Save yourself the headache get sql-compare from redgate. You shoukd be able to get a trial and test your scenaior. This tool plus their SQL toolbelt is a must for anyone dealing with multiple sql servers. http://www.red-gate.com/products/sql-development/sql-compare/

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.
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

Questions on SQL SERVER System objects

I have a couple of questions.
1) Why cannot we see system tables (like sysobjects) under Master/Model/MSBD etc.? But we
can query. Are we basically querying the views, because as they are the main tables that
holds a value able informations?
Like "SELECT * FROM sysobjects". are we basically querying some views?
2) Why cannot we add triggers to system tables?
Thanks in advance
SQL Server 2008 system tables (http://msdn.microsoft.com/en-us/library/ms179932.aspx) have been implemented as read-only views. One cannot directly work with the data in these system tables. You can access SQL Server metadata using catalog views. Do check this link http://msdn.microsoft.com/en-us/library/ms174365.aspx
It is possible to create triggers on system tables but it is generally not recommended. Please check this http://www.sql-server-performance.com/faq/trigger_system_table_p1.aspx
cheers
Since SQL 2005 the catalog views are implemented as views declared in the Resource Database (mssqlsystemresource). Due to some special magic they appear to exist in every database.
You can always use the execution plan to see from what actual tables do these views fetch data from. The underlying tables can be accessed when you are connected with a DAC connection. Modifying the system tables in any way will mark the database and an message will be logged every time the database starts up. Modified databases are not supported by MS, so if something goes wrong you cannot ask for support.

Resources