Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

How to Search a Column Name Within All Tables of a Database and How to Search Stored Procedures Containing a Particular Text

4.88/5 (24 votes)
23 Dec 2010CPOL 325K   1  
How to search a column name within all tables of a database and how to search stored procedures containing a particular text

1. How I find a particular column name within all tables of SQL server database?

For example, I have a database named Organisation. I have more than one table where tax_id column is present.

Most of the time, we have to find such a column from the whole database. The solution is provided below:

SQL
select table_name,column_name from information_schema.columns
where column_name like '%tax%'

Here, I am searching for column_name which contains tax within its name. It will return all the tables and respective columns containing tax.

Example:

Table_Name Column_name
t_Customer tax_id
t_Employee tax_number
t_Balance_sheet tax_percentage

2. How to search stored procedures containing a particular text?

Below is the solution. Suppose I need to find tax_id from all stored procedures. The below query will return all stored procedures containing text tax_id.

SQL
select routine_name, routine_definition
      from information_schema.routines
      where routine_definition like '%tax_id%'
      and routine_type='procedure'

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)