30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9: Searching DDL and Build Scripts<br />

SELECT @SearchString = '%' + @SearchString + '%' – -add it<br />

SELECT – -firstly, we'll search the names of the basic objects<br />

DB_NAME() + '.' + Object_Schema_name(s.[object_ID]) + '.'<br />

+ COALESCE(p.name + '.', '') + s.name AS [Qualified_Name] ,<br />

REPLACE(SUBSTRING(v.name, 5, 31), 'cns', 'constraint') + ' name'<br />

AS Object_Type ,<br />

s.create_date AS 'Created' ,<br />

s.modify_date AS 'Last_Modified' ,<br />

COALESCE(p.name, '-') AS 'parent'<br />

FROM sys.objects S – -to get the objects<br />

LEFT OUTER JOIN master.dbo.spt_values v – -to get the type of object<br />

ON s.type = SUBSTRING(v.name, 1, 2) COLLATE database_default<br />

AND v.type = 'O9T'<br />

LEFT OUTER JOIN sys.objects p – -to get any parent object<br />

ON s.parent_Object_ID = p.[object_ID]<br />

WHERE s.name LIKE @SearchString--string you want to search for<br />

AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%'<br />

UNION ALL<br />

SELECT--and search all the names of the columns too<br />

DB_NAME() + '.' + Object_Schema_name(s.object_ID) + '.' + '.'<br />

+ s.name + '.' + c.name AS [name] ,<br />

'Column name' AS [object_type] ,<br />

s.create_date AS 'created' ,<br />

s.modify_date AS 'Last Modified' ,<br />

COALESCE(s.name, '-') AS 'parent'<br />

FROM sys.columns c<br />

INNER JOIN sys.objects S – -get table data<br />

ON c.object_ID = s.object_ID<br />

WHERE c.name LIKE @SearchString--string you want to search for<br />

AND Object_Schema_name(s.object_ID) NOT LIKE 'sys%'<br />

UNION ALL<br />

SELECT--and search all the definitions of the computed columns too<br />

DB_NAME() + '.' + Object_Schema_name(s.object_ID) + '.' + s.name<br />

+ ',' + c.name AS [name] ,<br />

'computed Column definition' AS [object_type] ,<br />

s.create_date AS 'created' ,<br />

s.modify_date AS 'Last Modified' ,<br />

COALESCE(s.name, '-') AS 'parent'<br />

FROM sys.computed_columns c<br />

INNER JOIN sys.objects S ON c.object_ID = s.object_ID<br />

WHERE c.definition LIKE @SearchString--string you want to search for<br />

AND Object_Schema_name(s.object_ID) NOT LIKE 'sys$'<br />

UNION ALL – -now search the XML schema collection names<br />

SELECT DB_NAME() + '.' + name ,<br />

272

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!