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.
Related
unfortunately i create table call 'sysmessages' in SQL Server 2008. when i restore the DB to SQL Server 2012 i realize that i have two Tables call 'sysmessages'.
i don't want to change my table name because it using in the code.
can i remove only from specific database system table?
it is not a table, but a view
of course you cannot remove it, but you don't need to. It is in a different schema. You will not address it like select * from sys.sysmessages, you will address it like select * from dbo.sysmessages
"i don't want to change my table name because it is used in the code" - you can/should change the code as well :)
edit - no. 2. is not applicable in SQL 2012, however it is tested and working in SQL 2008R2
You cant drop system tables,your best bet is to change your code
A colleague of mine has given me a task to ensure all table name and pages in a stored procedure contains the relevant schemas?
For example if I have a table in a database that is dbo.table1, then in a query if I have:
Select * from table1
He wants me to change it to:
Select * from dbo.table1
This is same for pages that start with Support.
What is the significance of adding in a scheme like dbo. At the start when manually writing SQL? Is it suppose to be better for performance as it seems to know where the tables even if I don't include .dbo at the start?
I'm using SQL server 2012 and its management studio.
Thank you
You may not notice the performance if you write the schema in all of your queries. If you don't specify it, the engine will look through all of your schemas in your databases ( I'm not pretty sure if the engine will check the sys schemas too ) until it finds the table you're getting the data; It's recommended as a best practice to write it because you are telling to the engine what schema need to search the table.
I hope this answer was helpful.
dbo is the schema which is used as part naming convention for tables in a mssql database
look at this post SQL Server edits the table name with dbo as prefix
I've got a database that resides on an SQL server box, and another in a separate mdb file.
Both contain similar data, but I'd like to run a query that checks unmatched records from a field that exists in both.
Is this something that's easy enough to do using ADO (VBA)? If so can you point me in the right direction?
The easiest would be to create a new Access Database or ADP, and link the tables from both the SQL server and the other MDB. That way you've got an interface to both from within the same instance, which allows you to query or join different tables.
The company I work for uses a bunch of different SQL servers and I was wondering how to select a different SQL server in the same script.
For example, I want to select data from a table on a database in server 1 and using that data to get data from another table on a database in server 2. I tried googling the solution but I couldn't find anything relevant to my problem.
Thanks in advance.
You can set them up as linked servers.
http://msdn.microsoft.com/en-us/library/aa560998(v=bts.10).aspx
then you syntax will be
SERVERALIAS.DBNAME.owner.TABLE
Use fully qualified names (i.e. select * from [server].[database].[owner].[tablename])
Also, be sure to setup those servers as linked servers. There are several articles online how to do this.
I agree with Kyle & Flavio that You have to use four part naming convention to whatever server,database,table & column data like this:
Select * from [Servername].[Databasename].[Owner].[Tablename]
A cleaner option is to set up Synonyms to your linked servers. This way, you alias the server, and therefore don't have to hard code the 4 parts into every query.
If you hard code and later change a server's name, you will have to hunt down every reference and update. With Synonyms, all you'll have to do is update the applicable Synonym.
Synonyms give you transparent external tables, procedures, and UDFs.
MSDN here.
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.