I have a database with many tables and a set of queries which are going to be used for a particular application. The queries only use a small subset of the tables in the database and it has been decided to create a new database with just the set of tables which are referenced by the set of queries.
Is there a way to list the tables which are used by a set of queries?
The table names in this case are usually enclosed in square brackets so I can get so far by searching on this e.g. using 'Find In Files' in SQL Server management Studio. Is there a better way, perhaps using the fact that table names are usually preceded by the word 'FROM' or 'JOIN' ?
If these queries are stored procedures, you can right click on them in SSMS and select View Dependencies.
Once selected, the Object Dependencies dialog will show, and you will want to toggle the radio button to "Objects on which [Stored procedure Name] depends.
Another option is to generate an estimated query plan, and parse the XML that it generates.
Related
I need to find the tables and views used in stored procedure in which the tables and views may be of different databases.
Is there any way to find?
Eg: if i have sp "dbo.a"
i have to find the tables and views used within this.
The tables and views may be from different databases.
You can use sys.dm_sql_referenced_entities, e.g.
SELECT *
FROM sys.dm_sql_referenced_entities(N'dbo.YourProcedure', N'OBJECT');
There is probably no simple way to do this. What you need to do is find the table in your database which contains the code for all the stored procedures or some other way to read them.
Then you need to write an SQL parser which for your database. The parser can then read the source for the stored procedure and produce some data structure (usually an Abstract Syntax Tree) that you can traverse to find all the tables which it references.
I have a database whose structure I'm happy with, but which has a fair amount of dummy data in it. I would like to drop and recreate the database while at the same time wiping out the tables, but retain the table structures and relationships.
When I right-click on the database and choose to script 'drop and create' statements they're for the database itself, but no mention of the tables within-- unless I'm missing something.
Is it possible to generate a script that drops/creates a database and the tables within? I can individually select each table and script out their drop/create statements and order things so it will work, but are there other ways of doing this in one swoop?
I have a localized version of Sql Server 2008 R2, so some of my instructions could be imprecise.
I hope that there are no big differences.
Right click on your database and select Tasks and then Generate Scripts
Leave the first option selected (Build script for all db and objects) and select next
On the second page click Advanced
Select the option to Build Script for DROP & CREATE
Select the option to Use only the Schema (not Schema and Data)
Check the other options you would like to use.
Finally choose your save options and click Next until SSMS creates the script
Instead of that you should do this. That way you can select different tables or stored procedure to script for. See MSDN How to: Generate a Script (SQL Server Management Studio) on how to do it step-by-step.
Right click on DB_Name -> select tasks -> Generate Scripts
We have a lot of databases and a lot of tables within those databases. I'm searching for a specific one. I know the name of the table but it wouldn't be easy to search through every database manually. What SQL statement could I used to find the table by name?
Btw, we're using Microsoft SQL Server Management Studio. Maybe there's another way to search for tables by name within this program?
You said you did a search which should've led you to this article:
http://blog.sqlauthority.com/2008/04/29/sql-server-find-table-in-every-database-of-sql-server/
If not, follow that. Basically what he creates is a stored procedure which will search for every table name you specify in every database.
If you were to do this:
select * from sys.tables where name like '%tablename%'
You would need to change the database every single time and if you have a lot, well you see the problem.
Try this:
Select name from DBname.sys.tables where name like '%info'
Thought I would update with the solution I use now to find a table among many dBs. After some searching around I found this query:
/*Finds a table across multiple dBs and returns the dB(s) in which the table was found*/
SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'table name'
This query finds the dB which holds the table. Then, in Microsoft SQL Server Mgmt Studio, I go to Object Explorer Window, find the dB identified by the query, expand its contents, and click on the Tables folder. Then I use the Filter tool to find the table by name. It would be nice if the filter tool worked on the Databases folder but it does not. You must select the Tables folder before filtering.
This may not be the best solution, but it works for me.
So I have two Access databases, let's call them myDatabase and anotherDatabase. In anotherDatabase there is a crosstab query. I want to get the results that this query produces into myDatabase without making ANY modifications to anotherDatabase or the query itself. I want myDatabase to be enitrely self-sufficient in the sense that the databases it interacts with will not have to be modified.
Could anyone give me advice on how to approach this?
Linking external tables is limited to external tables and won't allow you to get data from queries in another db.
One solution for external queries is to create a local query using the IN predicate:
SELECT * FROM myQuery IN 'c:\test\otherdb.mdb'
Use the Linked Table Manager to link the tables in anotherDatabase to myDatabase. Then you can have the query for anotherDatabase in myDatabase and just use that.
In the External Data tab (in myDatabase), click the button that says Import From Access. Except, instead of importing the table, click the radio button that says "Link to the data source by creating a linked table". Just follow the wizard and you should be all set.
You only need to link the tables that you require from your query. Another option would be to write some vba code in myDatabase that instantiates a connection to anotherDatabase and queries it, but I think that just linking the tables is a better solution with less hassle
Is it possible to search and replace all occurrences of a string in all columns in all tables of a database? I use Microsoft SQL Server.
Not easily, though I can thing of two ways to do it:
Write a series of stored procedures that identify all varchar and text columns of all tables, and generate individual update statements for each column of each table of the form "UPDATE foo SET BAR = REPLACE(BAR,'foobar','quux')". This will probably involve a lot of queries against the system tables, with a lot of experimentation -- Microsoft doesn't go out of its way to document this stuff.
Export the entire database to a single text file, do a search/replace on that, and then re-import the entire database. Given that you're using MS SQL Server, this is actually the easier approach. Microsoft created the Microsoft SQL Server Database Publishing Wizard for other reasons, but it makes a fine tool for exporting all of the tables of a SQL Server database as a text file containing pure SQL DDL and DML. Run the tool to export all of the tables for a database, edit the resulting file as you need, and then feed the file back to sqlcmd to recreate the database.
Given a choice, I'd use the second method, as long as the DPW works with your version of SQL Server. The last time I used the tool, it met my needs (MS SQL Server 2000 / 2005) but it had some quirks when working with database Roles.
In MySQL, you can do it very easily like this:
update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');
I have personally tested this successfully on a production server.
Example:
update users set vct_filesneeded = replace(vct_filesneeded,'.avi','.ai');
Ref: http://www.mediacollege.com/computer/database/mysql/find-replace.html
A good starting point for writing such a query is the "Search all columns in all the tables in a database for a specific value" stored procedure. The full code is at the link (not trivial, but copy/paste it and use it, it just works).
From there on it's relatively trivial to amend the code to do a replace of the found values.