Jigar Shah Jigar Shah - 4 months ago 18
SQL Question

Sql Query with Brackets

I am making a query to lists down all stored procedures which contains an insert statement for a table.

Query doesn't give result if '[' is used in like statement.

SELECT Name,OBJECT_DEFINITION(object_id)
FROM sys.procedures WHERE OBJECT_DEFINITION(object_id) LIKE '%[dbo].[TABLE]%'


Problem is with '[', but not able to get it solved.

Answer

Try this:

SELECT Name,OBJECT_DEFINITION(object_id) 
FROM   sys.procedures WHERE OBJECT_DEFINITION(object_id) LIKE '%[[]dbo].[[]TABLE]%'

which escapes each instance of '[' by replacing it with '[[]'.

Comments