Search a table or view by name

Observed sign or problem

We know a word in the name—for example, “Order”—but we don’t know if the object is a table, a view, or what schema it is in. Opening folder by folder works on a small basis; on a legacy basis it soon ceases to be a reliable search.

What we need to distinguish

A schema is the namespace that groups objects inside a database, such as dbo or ventas. A pattern is a search form that may include wildcard characters.

First we want to locate objects whose name matches a pattern. It is also a good idea to distinguish tables from views and preserve the schema, because dbo.Pedido and ventas.Pedido are different objects.

Query

DECLARE @Patron SYSNAME = N'%Pedido%';

SELECT
    s.name AS Esquema,
    o.name AS Objeto,
    o.type_desc AS Tipo,
    o.create_date AS FechaCreacion,
    o.modify_date AS FechaModificacion
FROM sys.objects AS o
INNER JOIN sys.schemas AS s
    ON s.schema_id = o.schema_id
WHERE o.type IN ('U', 'V')
  AND o.is_ms_shipped = 0
  AND o.name LIKE @Patron
ORDER BY
    o.type_desc,
    s.name,
    o.name;

U represents a user-defined table and V represents a view. The is_ms_shipped = 0 filter prevents including objects marked as created by SQL Server internal components.

Parameters or permissions

Catalog views expose metadata: data that describes the structure of other objects.

Change @Patron. The signs % allow text to exist before or after: N'%Pedido%' finds Pedido, PedidoDetalle, and HistoricoPedidos. To search for the beginning of the name use N'Pedido%'; for an exact match, N'Pedido' and the operator =.

The query works on the active database. If there are several bases, you must execute it in each one or prepare a controlled search between bases. Catalog views only display metadata for objects that the account has visibility over.

How to read the result

Each row identifies a visible object. Always read Esquema and Objeto as a unit. Tipo confirms whether you are looking at a table or a view.

FechaModificacion serves as a hint, not a change history. In tables and views it can also change when an index is created or modified; by itself it does not prove that the logic or structure you are investigating has changed.

If you know the full name, you can confirm their identity without relying on a partial match:

SELECT OBJECT_ID(N'dbo.Pedido', N'U') AS IdTabla;

A value other than NULL confirms that a visible table with that two-part name exists in the current database.

What this does establish

Allows you to assert which visible tables and views in the current database match the pattern, along with their schema and type.

What this still does not establish

An empty result does not prove that the object does not exist on another base, under another name, or outside your permissions. It also does not include synonyms, external tables or objects whose relationship with “Order” only appears in a column or in its code.

Next reasonable check

When you find candidates, search their columns. If the name appears within procedures, views, or functions, locate the text within SQL objects. For registered relationships, check the dependencies and foreign keys.

Path index

Development and exploration

22 articles in Development and exploration
  1. 01Search for a table or view by name
  2. 02Search for a column across the database
  3. 03Search for text inside procedures, views, and functions
  4. 04Inspect the dependencies of an object
  5. 05List relationships and foreign keys
  6. 06Inspect primary keys, unique constraints, and indexes
  7. 07List columns with type, length, nullability, and default value
  8. 08Find triggers and their associated tables
  9. 09Find recently modified objects
  10. 10Get approximate row counts for every table
  11. 11Measure the space used by each table
  12. 12Inspect IDENTITY columns and their current value
  13. 13Find duplicate rows by business key
  14. 14Find orphaned rows
  15. 15Compare two data sets
  16. 16Validate conversions before changing a column
  17. 17Run an UPDATE with controlled validation and rollback
  18. 18Run a DELETE with a logical backup and controlled rollback
  19. 19Compare the structure of two databases
  20. 20Describe the result of a query or stored procedure
  21. 21Find tables without a primary key
  22. 22Search selected columns for a value