SQL query which stored procedure or trigger a content comes from

In the work of ERP operation and maintenance, I occasionally encounter some error prompts during the user's use of the system. At this time, what is the source of the error? How can I quickly track down the source of the error? Follow the error report. Content, find out which stored procedure or trigger in the system the prompt comes from.

--- View a content from a stored procedure

SELECT NAME FROM sysobjects o, syscomments s

WHERE o.id = s.id

AND TEXT LIKE '% error content%'

AND o.xtype = 'P'

If it is a trigger, change o.xtype to: TR

 

--Find the name of the stored procedure and content of the stored procedure

SELECT ROUTINE_NAME, ROUTINE_DEFINITION 

FROM INFORMATION_SCHEMA.ROUTINES 

WHERE ROUTINE_DEFINITION LIKE '% what you are looking for' 

AND ROUTINE_TYPE='PROCEDURE'

 

----------- View the trigger in which table a content comes from.

with TR AS (

SELECT name FROM sysobjects o, syscomments s

WHERE o.id = s.id  

AND TEXT LIKE '% error content%'

AND o.xtype = 'TR'

),

TR_TABLE AS (select triggers.name as TR_NAME,tables.name as TABLE_NAME,triggers.is_disabled as JY_YN,

triggers.is_instead_of_trigger AS TR_TYPE,

case when triggers.is_instead_of_trigger = 1 then 'INSTEAD OF'

when triggers.is_instead_of_trigger = 0 then 'AFTER'

else null

end as TR_MS

from sys.triggers triggers

inner join sys.tables tables on triggers.parent_id = tables.object_id

where triggers.type ='TR'

--order by triggers.create_date

)

select * from tr a

left join tr_table b on a.name=tr_name

Published 22 original articles · praised 7 · 100,000+ views

Guess you like

Origin blog.csdn.net/qyx0714/article/details/105440622