Validate that referenced tables exist when creating a procedure - sql-server

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

Related

Checking views and their default dependencies

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

MSAccess linked tables SQL Server "is not a valid name" error

I have a third-party database that i want to link to but many table names are illegal - so i'm looking for a workaround, possibly including just keeping a duplicate database with acceptable (legal) naming structure,
that anybody might have tried.
many thanks
Within a SQL database you can CREATE SYNONYM to alias an object/table name to something usable within Access. Create synonms for all of the tables and then use those as your linked table names.

SSMS how to find entity in database

I have been told an entity called table_loader in a database, database_1 in SSMS (version 2008 R2) exists and needs to be fixed. It is not obviously a stored proc. It's purpose is to convert an excel spreadsheet to table data. Is there any easy way to search a database for an entity name in SSMS.
The find function appears to work only with text SQL files as opened in SSMS.
Since originally posting I have found out from a colleague that this entity is a DTS package; however, I believe searching a database for a name is still a useful thing to be able to do, especially if you don't know what "layer" the entity is in with respect to the database folder structure.
Thanks.
A great, free, tool is Red-Gate SQL Search. It lets you search for just about any object from SSMS in a very user-friendly manner. http://www.red-gate.com/products/sql-development/sql-search/. You just type in the object name and it will search across databases and object types and display what it finds. I like it because it also searches within sproc text and such which can be very helpful, depending on what you're looking for.
If you open up a query window in SSMS you can use the below SQL to do a wildcard search:
USE [dbname]
SELECT * FROM sysobjects WHERE name like '%table_loader%'
This thread has got some good queries and lists the xtype meanings (sproc, table, key etc):
How do I get list of all tables in a database using TSQL?

how to handle db schema updates when using schemabinding and updating often

I'm using a MS SQL Server db and use plenty of views (for use with an O/R mapper). A little annoyance is that I'd like to
use schema binding
update with scripts (to deploy on servers and put in a source control system)
but run into the issue that whenever I want to e.g. add a column to a table, I have to first drop all views that reference that table, update the table, and then recreate the views, even if the views wouldn't need to be updated otherwise. This makes my update scripts a lot longer and also, looking the diffs in the source control system, it is harder to see what the actual relevant change was.
Is there a better way to handle this?
I need to still be able to use simple and source-controllable sql updates. A code generator like is included in SQL Server Management Studio would be helpful, but I had issues with SQL Server Management Studio in that it tends to create code that does not specify the names for some indices or (default) constraints. But I want to have identical dbs when I run my scripts on different systems, including the names of all contraints etc, so that I don't have to jump through loops when updating those constraints later.
So perhaps a smarter SQL code generator would a solution?
My workflow now is:
type the alter table statement in query editor
check if I get an error statement like "cannot ALTER 'XXX' because it is being referenced by object 'YYY'."
use SQL Server Managment Studio to script me create code for the referenced object
insert a drop statement before the alter statement and create statement after
check if the drop statement creates error and repeat
this annoys me, but perhaps I simply have to live with it if I want to continue using schemabinding and script updates...
You can at least eliminate the "check if I get an error" step by querying a few dynamic managment functions and system views to find your dependencies. This article gives a decent explanation of how to do that. Beyond that, I think you're right, you can't have your cake and eat it too with schema-binding.
Also keep in mind that dropping/creating views will cause you to lose any permissions that were granted on those objects, so those permissions should be included in your scripts as well.

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