HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How do I format the output from OBJECT_DEFINITION so that it looks the same as sp_helptext?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
formatthesameoutputobject_definitionlooksthathowfromsp_helptext

Problem

object_definition and sp_helptext both return the source code of a stored procedure - but how can I use object_definition and get the source code including the line breaks as the sp_helptext currently does?

for example in the code below I create a stored procedure:

if OBJECT_ID('usp_radhe') is not null
   drop procedure usp_radhe
go
-- this procedure is just a test
-- it just returns a date in the past
-- how will I get its source code?
create procedure usp_radhe as 
begin
select dateadd(dd,-31,GETDATE()) 
end


using sp_helptext I get a nice view of the source code:

sp_helptext 'usp_radhe'


using object_definition I get the source code in a single line and that is not good for me:

select OBJECT_DEFINITION(object_id('usp_radhe'))

Solution

I had similar problem. First turn on this option of SSMS - it is turned off by default:

SSMS/Tools/Options/Query results/SQL Server/Results to grid/Retain CR/LF on copy or save


Close SSMS, and run it again to get it work.

I use this query to get the full text of object definitions:

create view v_get_text_of_objects as

select top 100000
  [name]
, definition = replace(replace(object_definition(object_id),'create view','alter view'),'create proc','alter proc')
, len = len(object_definition(object_id)) 
, type
, type_desc
, create_date   
, modify_date
from  sys.objects
where object_definition(object_id) like '%your text to find in sp%'
order by type, create_date desc


I like to display LEN of the sp text, so that I am sure it is not cropped.

Code Snippets

SSMS/Tools/Options/Query results/SQL Server/Results to grid/Retain CR/LF on copy or save
create view v_get_text_of_objects as

select top 100000
  [name]
, definition = replace(replace(object_definition(object_id),'create view','alter view'),'create proc','alter proc')
, len = len(object_definition(object_id)) 
, type
, type_desc
, create_date   
, modify_date
from  sys.objects
where object_definition(object_id) like '%your text to find in sp%'
order by type, create_date desc

Context

StackExchange Database Administrators Q#191311, answer score: 6

Revisions (0)

No revisions yet.