This question already has answers here:
Search for a string in all tables, rows and columns of a DB
(15 answers)
Closed 9 years ago.
I've been scouring online for an example of how to do this but haven't found anything at all. All the queries I've found assume you know what table you want to search.
I'm looking for a SQL query to simply search the ENTIRE database for a specific word.
There has to be such a thing right?
This is for MS SQL 2005/2008
Thanks
What do you mean by "entire database"? You need to find your values in tables only, or in object definitions, too?
I assume the former. In this case, you don't have to really know the structure of your DB. Try those views below. With them, you can construct your select queries on all the tables / colums. Just filter out non-*char columns, views and system tables, and you're ready to go - you can "automatically" generate multiple select statements.
select top 100 * from information_schema.tables
select top 100 * from information_schema.columns
The other option, is to use some addon to SSMS, like this one:
http://www.ssmstoolspack.com/
It has an option to search entire database.
But be advised, that both solutions will have a great impact on performance of your server.
Related
This question already has answers here:
How to generate entire DDL of an Oracle schema (scriptable)?
(7 answers)
Closed 2 years ago.
I want to create tables from one schema to another. i.e. complete table script.
eg: Let say in schema 'A' tables are present and need to create in schema 'B'.
Can we do this with a scripts as there are lot of tables? or manually is the only option?
Could anyone pls suggests.
Thanks.
Yes it can be done by a script. Below, you will find the example of how to retrieve the DLL of a single Table. Now, using this example, you can create the script in no time.
SELECT DBMS_METADATA.get_ddl ('TABLE', table_name, owner)
FROM all_tables
WHERE owner = 'OWNER'
AND table_name = 'TABLE_NAME';
If you want the script directly, the following Link will provide you with your desired result:
How to generate entire DDL of an Oracle schema (scriptable)?
Working in SQL Server 2008 where I only have access to create queries, not tables or views. My question is related to structuring code better.
I have views provided that give me multiple data sets with too much information. I'm attempting to slim the data down for easier use and reference, with requested specifics.
My goal is to have (3) main queries that list all of the data that I need, and to reference and reuse the data for multiple reports.
Query 1 - Incidents: lists all of the detail on a call
Query 2 - Units: lists all of the detail on units responding to a call
Query 3 - 1stUnits: lists all of the detail on 1st responding units only.
Because each of these queries will have multiple CASE statements within them, I was looking for a way to name the actual query, and reference the query in SQL as Qry1Calls or Qry2Units without an extensive subquery, within a query.
I've searched a bit, and found nothing.
It would be a lot easier to select Qry1 and Qry3 where id's match and filter those results by parameters.
Is this possible? I'm transitioning from Access with some SQL to full fledge SQL.
What you are looking for is probably Common Table Expressions (CTEs)
Read about them here:
https://msdn.microsoft.com/en-us/library/ms190766(SQL.90).aspx
Reference for the with keyword and examples here:
https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql
This question already has answers here:
How to find stored procedures by name?
(4 answers)
Closed 8 years ago.
I have a large number of stored procedures in my database, and I would like to see if one of those would accomplish my task before I write a new one. How would I search through just the names of the existing stored procedures (as opposed to the contents)?
SQL Server 2005+:
SELECT name FROM sys.procedures WHERE name LIKE '%search_text%';
SQL Server 2000:
SELECT name FROM sysobjects WHERE name LIKE '%search_text%' AND xtype = 'P';
Another good idea is to establish a sensible naming schema. You shouldn't have to search at all if your naming convention is consistent and logical.
SELECT DISTINCT OBJECT_NAME(id)
FROM syscomments
WHERE OBJECT_NAME(id)
LIKE '%search_text%'
SELECT name
FROM sys.objects
WHERE type = 'P'
AND name LIKE '%search_text%'
The above methods all work great but an easier approach is to download a copy of SQL Search from Red Gate. It's a free utility that plugs into SSMS and allows you to search on all objects throughout the entire server.
This question already has answers here:
Different ways to alias a column
(3 answers)
Closed 8 years ago.
SELECT a=1,b=2
vs
SELECT 1 AS a, 2 AS b
what approach should i use ? (by best practice decisions)
Is there any advantages of one vs the other ?
Personally I use AS - it is more explicit and used elsewhere (table aliases).
It is also standard - using the = syntax will not work on some databases.
I don't believe there are any performance implications for using one over the other.
I dont think where is a difference. But i would prefer the second approach. I think its more logical than the first.
So, i use the AS approach.
AS temporarily assigns a table column a new name. This grants the SQL developer the ability to make adjustments to the presentation of query results and allow the developer to label results more accurately without permanently renaming table columns. Source
I use AS. It's a more SQL friendly way!
SELECT 1 AS a, 2 AS b
is common for all DBs
This question already has answers here:
Find a value anywhere in a database
(18 answers)
Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql statement
(21 answers)
Search all tables, all columns for a specific value SQL Server [duplicate]
(4 answers)
Closed 8 years ago.
My question might sound stupid, but does there exist a script that could find out where in database, for which tables, is the desired data located? Say for example, i need to found where Texas is located in database, in which tables and in which column.
There exists a script that could find out the tables, SP, views based on the column name provided. Is there any script that could find out tables, column name etc based on the actual data?
Hope the question is understood.
Best Regards
Josh Walker has a script that will find the number of incidences a string of text is found, and in which tables:
http://www.sqlservercentral.com/scripts/Miscellaneous/65769/
And, this statement should find any procedure code that contains the text you are looking for:
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%whatever%'
The SSMS Tools Pack gives you this kind of search functionality plus various other cool things for free. And no, I don't work for them!
I have got one great tool that can find data anywhere located in the database. It is known as SQL Locator. Just Google it, that should be easy to find. Plus it is a freeware to download.
Thanks!