Tuesday, November 30, 2004

Stored procedure to find all instances of a field

As a report writer, database administrator, developer or user, it may be useful to know which table a field exists in. Many third party vendors don't supply a data schema, making it difficult to query and build reports. This stored procedure is a simple method of searching through all the table in the current database for a field that you specify.

While you certainly cannot assume that because two tables have the same field that they are related, this will certainly be at least one tool you can use to help find those relationships.

CREATE PROCEDURE dbo.sp_FieldInfo
(
@Column_Name nvarchar(384) = NULL
)
AS
SELECT Object_Name(id) as 'Table Name',
rtrim(Name) as 'Field Name'
FROM syscolumns
WHERE name like @Column_Name
GO

Then to call this proc:
sp_fieldinfo employeeid

1 comment:

Anonymous said...

Thank you, very helpful! :)