Search stored procedures/functions in all databases - sql

I want to search for specific text in all procedures/functions etc. in all databases. I managed to create the required query from this answer but it looks like OBJECT_DEFINITION(OBJECT_ID(SPECIFIC_NAME)) returns NULL for all DBs except the current one.
sp_msforeachdb 'SELECT ''?'' AS DB, SPECIFIC_NAME, OBJECT_DEFINITION(OBJECT_ID(SPECIFIC_NAME)) FROM [?].INFORMATION_SCHEMA.ROUTINES'

You absolutely need Red-Gate's SQL Search tool - it's FREE, and absolutely great and perfectly suited for this need.

The problem is OBJECT_ID cannot be used that way. It only works on the current database. Try returning ROUTINE_DEFINITION directly from INFORMATION_SCHEMA.ROUTINES. This does have a limit of 4000 characters. I'll try to find my other answer on SO which gives my workaround using the MS metadata views.
Have a look at this:
Can you search SQL Server 2005 Stored Procedure content?

try this:
select * from syscomments where [text] like '%yourKeyword%'

Related

Need to identify a table in all objects in database

I need to identify a table that is mentioned anywhere in database (in stored proc, views, and, etc.). I tried to find a query online, but couldn't find it. Any help would be great!
I use the free SQL Search plugin for MS Management Studio for things like that: http://www.red-gate.com/products/sql-development/sql-search/
I often use this snippet when I'm looking for dependencies. In this case, you would replace the text with what you're searching (assuming you're on MS SQL Server):
USE [DBNAME]
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%enter_search_here%'
GROUP BY OBJECT_NAME(id)
You can also look for specific object types by adding a check for object property:
WHERE OBJECTPROPERTY(id, 'IsTable') = 1
Here is a LIST of useful object properties!

sql or trick to search through whole database

is there a way to actually query the database in a such a way to search for a particular value in every table across the whole database ?
Something like a file search in Eclipse, it searches accross the whole worspace and project ?
Sorry about that .. its MS SQL 2005
SQL Workbench/J has a built in tool and command to do that.
It's JDBC based and should also work with SQL Server.
You will need to use the LIKE operator, and search through each field separately. i.e.
SELECT * FROM <table name>
WHERE (<field name1> LIKE '%<search value>%') OR
(<field name2> LIKE '%<search value>%') OR
... etc.
This isn't a quick way though.
I think the best way would be to
1) programatically generate the query and run it
2) use a GUI tool for the SQL server you are using which provides this functionality.
In mysql you can use union operator like
(SELECT * from table A where name = 'abc') UNION (SELECT * from
table B where middlename = 'pqr')
and so on
use full text search for efficency
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Well, your best bet is to write a procedure to do this. But to give you some pointers you can use the INFORMATION_SCHEMA.Tables to get a list of all the tables in a given database and INFORMATION_SCHEMA.Columns to get a list of all columns. These tables also give you the datatype of columns. So you will need a few loops on these tables to do the magic.
It should be mentioned most RDBMSs nowadays support these schemas.
In phpmyadmin, go to your database, reach the search tab.
Here you will be able to select all of your tables and search through your entire db in one time.

Wildcards in SQL parameter

I need to do a like search, in my SQL Server database. BUT, how do I do this, while still using parameters for my search value?
I am not talking about encapsulating in %'s. I need to be able to add a % in the middle of the search word.
WHERE title LIKE '%foo%bar%'
as an example, but with params.
Edit:
To elaborate on the params:
I am using MS SQL 2008, and C#, so it would be:
WHERE title LIKE '#SearchParam'
and #SearchParam would then be set to "%foo%bar%".
I hope that makes sense?
This works fine
create proc dbo.lookupWild(#LIKEclause VARCHAR(20))
as
begin
select * from teams where Name like #LIKEclause
end
go
exec lookupWild 'E%G%'
And things like '1=1 -- drop table' are not SQL injected, they are just part of the wildcard search
This should work fine if you are using Stored Procedures. Here is a similar thread.
How to Escape Wildcard Characters While Searching in SQL Server
Are you looking for something like below?
where title like '%foo[%]bar%'

How to search for a specific string in all columns within all tables of a SQL Server database?

We want to search for a string (ie. "Hello World") in all our database that has about 120 tables. We thought about doing a dump like mysql dump but it came out in a weird bak format.
The search should be done in each column for each table. Is this possible with any type of script, or this is harder than it sounds to me?
How to search all columns of all tables in a database for a keyword
No it possible and easy to write a script to do this.
Suggestions:
I think you have to use some cursors and use some of these objects to write your script
sys.databases
INFORMATION_SCHEMA.TABLES or sys.tables
INFORMATION_SCHEMA.COLUMNS or sys.columns
Once you have these things in place, searching Hello World under all columns would be more simple
Is this just for a one-off, or something you want to do regularly?
If it's a one-off, how about using the export data wizard to export the tables out to CSV files (assuming you're using SQL Server, although I'm sure most databases have equivalents).
Once you've done this you can just do a 'Find Files' in explorer to find all occurrences?
It's a bit dirty - but it'll work!

SQL to search objects, including stored procedures, in Oracle

I need to write some sql that will allow me to query all objects in our Oracle database. Unfortunately the tools we are allowed to use don't have this built in.
Basically, I need to search all tables, procedures, triggers, views, everything.
I know how to search for object names. But I need to search for the contents of the object.
i.e. SELECT * FROM DBA_OBJECTS WHERE object_name = '%search string%';
Thanks,
Glenn
I'm not sure I quite understand the question but if you want to search objects on the database for a particular search string try:
SELECT owner, name, type, line, text
FROM dba_source
WHERE instr(UPPER(text), UPPER(:srch_str)) > 0;
From there if you need any more info you can just look up the object / line number.
For views you can use:
SELECT *
FROM dba_views
WHERE instr(UPPER(text_vc), UPPER(:srch_str)) > 0
i'm not sure if i understand you, but to query the source code of your triggers, procedures, package and functions you can try with the "user_source" table.
select * from user_source
I would use DBA_SOURCE (if you have access to it) because if the object you require is not owned by the schema under which you are logged in you will not see it.
If you need to know the functions and Procs inside the packages try something like this:
select * from all_source
where type = 'PACKAGE'
and (upper(text) like '%FUNCTION%' or upper(text) like '%PROCEDURE%')
and owner != 'SYS';
The last line prevents all the sys stuff (DBMS_ et al) from being returned. This will work in user_source if you just want your own schema stuff.
i reached this question while trying to find all procedures which use a certain table
Oracle SQL Developer offers this capability, as pointed out in this article : https://www.thatjeffsmith.com/archive/2012/09/search-and-browse-database-objects-with-oracle-sql-developer/
From the View menu, choose Find DB Object. Choose a DB connection. Enter the name of the table. At Object Types, keep only functions, procedures and packages. At Code section, check All source lines.
ALL_SOURCE describes the text source of the stored objects accessible to the current user.
Here is one of the solution
select * from ALL_SOURCE where text like '%some string%';
In Oracle 11g, if you want to search any text in whole database or procedure below mentioned query can be used:
select * from user_source WHERE UPPER(text) LIKE '%YOUR SAGE%'