How to know that how many database tables, views or any other object attached to a view in sql server 2005 database.
I tried with Sp_Depends, but amazingly it is not showing the SP name in which it is getting used.
When I run the statement Sp_depends vw_MyViewName.
I am only getting the name of the tables and columns which I have used inside the vw_MyViewName. I need to know other objects related to this view.
In SQL Server 2005 this happens if you create the objects in the wrong order.
You will get a warning message that the dependency information could not be added for a missing object but the object will still be created
You can run sp_refreshsqlmodule on all objects in your database to recreate such missing dependency information (an example script to do that is here How do I find all stored procedures that insert, update, or delete records?)
Until sys.sql_expression_dependencies was introduced (SQL Server 2008), you need query sys.sql_modules
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE definition LIKE '%MyTable%'
Personally, I'd use WITH SCHEMABINDING to ensure dependencies must exist
The only completely reliable way I know of to determine object dependencies in a SQL Server database is to load your schema into Visual Studio Database Edition (DBPro) and examine the dependencies there. I have found this to be foolproof, unlike the way SQL Server tracks dependencies.
I wouldn't necessarily fault SQL Server for this. I don't think it ever made the claim that it was able to track dependencies with 100% accuracy, mostly because of the way it binds objects.
In SQL Server Management Studio, in Object Explorer panel, right click on the object you want to inspect (stored procedure, table, view, ...) and click on "Show Dependencies".
The window that appear, will show you both dependant and "depended" objects, simply by switching between two radio button ;)
If you want to do it by hand, you need to build a query over the sys.sql_dependencies system view. Here a link to the description with some examples
Beware that stored procedures that depends on nonexistants tables, view, and other objects, will be created, but not only they obviously doesn't work, but dependency informations will not be added, until all "depended" objects are created, AND the SP is REcreated!
Related
Wondering if this is possible.
I'm working on ironing out some source control differences between our database environments using RedGate SQL Source Control.
A couple of procedures that I am trying to script rely on some Linked Servers. While I have been able to script the Linked Servers with default passwords (so they do exist, at least technically), they have some unknown provider (it's listed as an OLEDB Provider, with no indicator of what specifically it is).
Bottom line is these all fail because of these Linked Server references in the stored procs ("Data source name not found and no default driver specified").
While I track this down, I would like to at least create the stored procedures like views are, even though the dependency can't be resolved, so that SQL Source Control no longer displays these as New changes that need to be pulled down.
I don't want to ignore them because they do need to be fixed (which is why I am choosing not to add them to the RedGate tool's Filters).
Is it possible to force creation of the stored procedure in spite of the missing dependency?
I'm new to using SQL Server 2008 DB Project's in VS 2010. I found a good intro to setting them up. It's nice how they create Tables, Stored Proc's etc as objects. But is it also a limitation?
I want to use this project to manage 1 stored procedure (for learning). I do not want to import the entire database because 90% of the database is stuff we do not manage.
I created a new project without doing the import process. I then added a new stored procedure. Now I am having difficulty getting the thing to build. I'm getting various errors saying that I have unresolved references to objects.
How can I add a new stored procedure..build it and deploy it to the database? Is it possible with this kind of SQL project or do I need to drop back to the old, simple type of SQL projects that VS 2008 and below used?
Update
According to another post, support for the Database Project type is gone. Support for my situation appears to have been erased.
UPDATE 2 3/21/2012
I installed MSSCCI which allows me to use SSMS directly with TFS 2010. I no longer needed and found the setup process to be unmanageable for a large database SQL 2008 project. Especially when you only manage a small % of the DB.
You can Partition a Database Project by Using Partial Projects. This allows the database project to know the entire schema of the database, at the same time, you need not maintain the entire schema. You can work with the subset of the database that's under active development, for instance (or the subset which is your responsibility), yet the project knows the entire schema. This permits it to create change scripts at deployment time, by comparing the schema in the project with the schema in the target database.
You must import all schema objects referenced by your new stored procedure. But this can become a large task because every referenced object need all it's references too.
More trouble with linked server objects.
I'm working on a project which relies on the presence of a number of tables, views and stored procedures. Until now I have built these all in SQL Server Management Studio.
Now I would like to continue to work on them inside of Visual Studio. This will provide the benefit of version control (along with a number of other benefits hopefully).
I have added a new project to my solution and started working on one of the views. When I tried to build the solution it failed as the new project didn't have a server/database associated: Error 1 SQL03006: View: [dbo].[vw_Test2] has an unresolved reference to object [EV870_ACCT_MASTER].
I was able to overcome this by
-creating a dbschema dump using vsdbcmd.exe
-adding the dbschema dump as a reference to my database project
Is this the correct approach?
Now i can see the schema (tables, views, sprocs etc) in the Schema view (I had to enable display of "external elements") and the error message has gone away. Note: I had to reference like: [$(SQLDatabase)].[dbo].[EV870_ACCT_MASTER]
Now I want to know how I can work with these objects that i've scripted. I don't know how to use the new tables, views, sprocs etc (I want to use LINQ). Do i have to run the scripts first? How then if they are "CREATE OBJECT" scripts, will they run in future (presumably they'd fail as the object already exists in the database). Will my project/solution know which objects need updating and update them?
Ultimately want to take it a lot further- my aim is that the solution will be portable and a the server/database will be configurables. Then my tables, views and stored procedures will be created or amended if they don't exist or are out of date. Is this possible?
When I then start working with the views etc using LINQ I want those server/database references to remain dynamic?
I know there are quite a few questions in there but i'm hoping someone will be able to point me in the right direction- there doesn't seem to be much useful documentation online (or that i've stumbled across so far).
Thanks
Lee
Where I work (and the last place I worked) we distribute the sql scripts to create the database along with the app. In sql a version number is stored and when the app is run it checks to see if its version is newer than the number stored in the database. If so then it knows it may need to run some new sql scripts in case there were any schema changes. When this happens, we just run through all the scripts because they are written in a way that running them multiple times won't hurt anything... this way we don't have to worry about tracking which scripts are the new ones. Just check the version number and that's it.
As far as working with this stuff in Visual Studio instead of Management studio, I'm not sure why anyone would want to do that. Depending on what you use for source control you may be able to get hooks for Management Studio, but even if not that doesn't stop you from keeping your sql scripts in source control. And I wouldn't switch from working with my sql files in management studio to visual studio for the benefit of having built in source control any day.
Is there anything built into the Linq to SQL libraries that allow me to create an entire database from a collection of DDL files?
Lets say you have a collection of DDL files, one for each table in the database. Conceptually it would be pretty straight forward to call a create table function for each one and each one that succeeds (does not through SQL exception for example due to a relationship or foreign key error) pop the file name off the stack. For any that failed you could try to call the DDL again until it finally succeeded and all of your tables existed in the database ... however ... if there is something like this that already existed in say Linq to SQL or the Migrations project that would be great. Does anyone know if this exists already without having to combine all of the DDL's into a single script? Thanks in advance.
If you have Visual Studio 2008 or 2010 Professional or Above, it includes the new version of database projects. Which can handle that precisely for you (it will even validate the scripts before execution so you can see what errors exists).
I don't believe so. Linq-to-Sql is not really made for manipulating database schemas. You might have more luck with something like the Microsoft SMO libraries.
Use ADO.NET commands instead for that. That should be able to handle it, depending how complex each file is. As long as each file has on executable statement, ADO.NET commands may work fine for what you want to do.
In a project on which I am performing maintenance, I am referencing a database stored on another server. This reference was previously pointing at a database on the same server, but my testbed server isn't large enough to store both databases, so I'm having to refer back to the live database.
Anyway, whoever created this application made use of SQL Views, and I have found that when trying to edit the view, that it cannot reference an external database.
The existing reference is to myDatabase.dbo.tableName. When I try to change that to [MyServer.com].myDatabase.dbo.tableName moving away from the editor removes all of the square brackets, leaving me with MyServer.com.myDatabase.dbo.tableName which is not a proper table reference.
Any ideas what I can do to fix this?
It looks like you have added a linked server with the name "MyServer.com" I suggest you rename this to something without the dot, MyServerCom would be fine. Its ok if the details in the linked server point to a server that happens to be at that address.
Then the four part name can be MyServerCom.myDatabase.dbo.tableName
Don't edit the view definition in Enterprise Manager - use query analyser to script the view for ALTER, make the appropriate changes, and then run the script.
Enterprise manager does some fairly nasty things. I'd similarly recommend scripting an ALTER rather than using the "Design" option if you're using SSMS to manage your server/database.