从系统视图sys.syscomments里可以得到存储过程的项,text字段记录了存储过程定义的实际文本,如果存储过程的长度超过4000长度,将使用新行记录,colid字段记录相应的行序号.
以下语句使用XML结合value()方法将同一存储过程的多行text字段合并成一列显示:
select
object_name
(id)procedure_name,
text
+
isnull
(
(
( select text from sys.syscomments b where a.id = b.id and b.colid > 1 for xml path( '' ),root( ' root ' ),type)
.value( ' /root[1] ' , ' nvarchar(max) ' )
), '' ) procudure_text
from sys.syscomments a where number = 1 and colid = 1
(
( select text from sys.syscomments b where a.id = b.id and b.colid > 1 for xml path( '' ),root( ' root ' ),type)
.value( ' /root[1] ' , ' nvarchar(max) ' )
), '' ) procudure_text
from sys.syscomments a where number = 1 and colid = 1