When working with an older or unfamiliar SQL Server database, you may encounter a name inside a stored procedure that does not appear to exist as a normal table column. The name may be a column alias, a computed column, or a reference used by another database object.
For example, a stored procedure may return or search against a name such as: SearchColumn
Copy
Search Site
Search Google
Real World Example
I was working on our Radio site when I was trying to upload files to the live server. I noticed that when I looked in the database for the Artist, something was off. I knew I had added several artists a few months ago, but where did they go?
I then opened the old database, which was still connected to the old [ AOAG ]. Once I opened the Artist table, I saw the new artist I had added. So, that started me on the journey of doing the following.
  1. Find out which of the two database tables has the most records.
  2. Copy the data over from the updated table to the original table, since it had all the new artists.
  3. Add in any new tables to the original database (9 new Tables)
  4. Then drop the updated database, and restore the original database with all the updated information.
After performing the above, I am happy to report that I now have the updated database with all records.


Before changing the stored procedure or adding a new column, you should search the database to determine where that name is defined and which objects depend on it.
The following six searches cover the most common SQL Server object types.
1. Search Stored Procedures and Other SQL Modules
The sys.sql_modules
Copy
Search Site
Search Google
catalog view contains the definitions of stored procedures
Copy
Search Site
Search Google
, views
Copy
Search Site
Search Google
, functions
Copy
Search Site
Search Google
, and triggers
Copy
Search Site
Search Google
.
[SQK Server - SQL text]
Use the following query to search their SQL text:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    OBJECT_SCHEMA_NAME(object_id) AS SchemaName,
    OBJECT_NAME(object_id) AS ObjectName
FROM sys.sql_modules
WHERE definition LIKE '%SearchColumn%';

This is usually the best first search because it can quickly locate any stored procedure or other programmable object containing the requested text.
The search is performed against the complete SQL definition stored by SQL Server.
[Possible results may include:]
  • dbo.sp_SearchProductsPaging
  • dbo.sp_GetProductsearch
  • dbo.vw_Productsearch
This query identifies the objects containing the name, but it does not show the object type. For that, use the broader search shown in section six.
2. Search SQL Server Views
A view may expose a renamed or calculated value that does not physically exist in the underlying table.
[SQK Server - View Definitions]
Use the following query to search view definitions:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    TABLE_SCHEMA,
    TABLE_NAME
FROM INFORMATION_SCHEMA.VIEWS
WHERE VIEW_DEFINITION LIKE '%SearchColumn%';

[SQK Server - Alias]
A view may contain an alias such as:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    CategoryName AS SearchColumn
FROM dbo.Products;

In that example, SearchColumn is not a physical column. It is only the name assigned to CategoryName in the view results.
Be aware that INFORMATION_SCHEMA.VIEWS may not always return the complete definition for very large or encrypted views. Searching sys.sql_modules is generally more reliable.
3. Search SQL Server Functions
SQL Server functions can return calculated values, table results, or reusable search expressions.
[SQK Server - Scalar and Table-Valued Functions]
Use the following query to search scalar and table-valued functions:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    OBJECT_SCHEMA_NAME(o.object_id) AS SchemaName,
    o.name AS ObjectName,
    o.type_desc AS ObjectType
FROM sys.objects AS o
INNER JOIN sys.sql_modules AS m
    ON o.object_id = m.object_id
WHERE o.type IN ('FN', 'IF', 'TF')

  AND m.definition LIKE '%SearchColumn%';

[The function types are:]
  • FN = SQL scalar function
  • IF = Inline table-valued function
  • TF = SQL table-valued function
[SQK Server - Function]
A function may build a searchable value by combining several columns:
CFFCS | CarrzSynEdit: | SQL Script
RETURN CONCAT(@ProductName, ' ', @CategoryName);

A stored procedure could then call that function without directly showing how the value was created.
4. Search SQL Server Triggers
A trigger may populate, modify, validate, or reference a value when rows are inserted, updated, or deleted.
[SQK Server - Trigger Definitions]
Use this query to search trigger definitions:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    OBJECT_SCHEMA_NAME(object_id) AS SchemaName,
    OBJECT_NAME(object_id) AS TriggerName
FROM sys.sql_modules
WHERE definition LIKE '%SearchColumn%'

  AND OBJECTPROPERTY(object_id, 'IsTrigger') = 1;

[SQK Server - Trigger]
Triggers are easy to overlook because they execute automatically.
For example, a trigger could update another search table after an album is inserted:
CFFCS | CarrzSynEdit: | SQL Script
UPDATE dbo.Productsearch
SET SearchColumn = i.CategoryName
FROM inserted AS i;

When investigating unexplained data behavior, always check whether triggers are involved.
5. Search Table and Computed Columns
A computed column is a real column definition stored in the table schema, but its value is calculated from an expression rather than entered directly.
[SQK Server - Table Columns]
Use the following query to search all table columns:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    OBJECT_SCHEMA_NAME(object_id) AS SchemaName,
    OBJECT_NAME(object_id) AS TableName,
    name AS ColumnName
FROM sys.columns
WHERE name = 'SearchColumn';

If this query returns a result, the column exists in the table schema.
[SQK Server - Computed Column]
To determine whether it is a computed column, use:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    SCHEMA_NAME(t.schema_id) AS SchemaName,
    t.name AS TableName,
    c.name AS ColumnName,
    cc.definition AS ComputedDefinition,
    cc.is_persisted AS IsPersisted
FROM sys.computed_columns AS cc
INNER JOIN sys.columns AS c
    ON cc.object_id = c.object_id
   AND cc.column_id = c.column_id
INNER JOIN sys.tables AS t
    ON cc.object_id = t.object_id
WHERE c.name = 'SearchColumn';

A possible computed definition could be:
([ProductName] + ' - ' + [CategoryName])
Copy
Search Site
Search Google
The column appears like a normal column in queries, but SQL Server calculates its value from the source columns.
6. Search All SQL Module Types
The following query searches all objects stored in sys.sql_modules and also displays the object type:
[SQK Server - Searches all Objects]
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    o.type_desc AS ObjectType,
    OBJECT_SCHEMA_NAME(o.object_id) AS SchemaName,
    o.name AS ObjectName
FROM sys.sql_modules AS m
INNER JOIN sys.objects AS o
    ON m.object_id = o.object_id
WHERE m.definition LIKE '%SearchColumn%'

ORDER BY
    o.type_desc,
    o.name;

This is one of the most useful general-purpose searches because it can locate the name inside:
  • Stored procedures
  • Views
  • Scalar functions
  • Table-valued functions
  • Triggers
  • Other SQL modules
It also identifies the object type, making the results easier to investigate.
[The Name May Only Be an Alias]
A name found in a stored procedure is not always a table column.
[SQK Server - alias]
It may simply be an alias:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    CategoryName AS SearchColumn
FROM dbo.Products;

[SQK Server - calculated expression]
It could also come from a calculated expression:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    CONCAT(ProductName, ' - ', CategoryName) AS SearchColumn

FROM dbo.Products;

[SQK Server - CASE expression]
From a CASE expression:
CFFCS | CarrzSynEdit: | SQL Script
SELECT
    CASE
        WHEN AlbumTitle IS NULL THEN ProductName
        ELSE ProductName + ' - ' + AlbumTitle

    END AS SearchColumn
FROM dbo.Products;

Aliases exist only in the query results unless they are part of a view, temporary table, common table expression, or another stored object.
[Recommended Search Order]

When investigating an unfamiliar name, use this order:
  1. Search sys.sql_modules.
  2. Search sys.columns.
  3. Search sys.computed_columns.
  4. Open every matching stored procedure or view.
  5. Look for AS SearchColumn.
  6. Check functions and triggers if the source is still unclear.
This process helps determine whether the name is:
  • A physical column
  • A computed column
  • A query alias
  • A view column
  • A function result
  • A trigger reference
  • A stored procedure search value
Searching the database before making schema changes can prevent duplicate columns, broken procedures, and unnecessary modifications.
Other Articles Related to this Entry.