Update
With the release of SSMS 18.2 on 2019-07-25, a new option became available. The option itself has existed for a long time, but with a change made in this release it became viable to use for the purpose requested here.
The specific change was increasing the number of characters retrieved when using "Result to Text" (by 8x). The documentation states (emphasis mine):
Allow more data to be displayed (Result to Text) and stored in cells (Result to Grid). SSMS now allows up to 2M characters for both (up from 256 K and 64 K, respectively). This also addressed the issue of users not able to grab more than 43680 chars from the cells of the grid.
Now the following displays as expected in "Results to Text":
SET NOCOUNT ON; SELECT OBJECT_DEFINITION(OBJECT_ID(N'sys.sp_helptext'));
By default, the results are "column aligned" and "include column headers". With this combination of options, a line of dashes is used between the column headers and the results. The problem is that being column aligned, the dashes extend the full amount of what that column could possibly retrieve, which is likely to be 2,097,152 dashes (i.e. 2 MB worth). SSMS doesn't like super long lines and freezes for a second when scrolling up for that line to be in view.
You can get rid of that line by changing either (or both) of those two aforementioned options: either uncheck "Include column headers in the result set" and/or set "Output format" to "comma delimited". If you want these changes to be the new defaults (i.e. be remembered for next time), then you need to make the changes by going to Tools -> Options -> -> Query Results -> SQL Server -> Results to Text. If you want the changes to only be temporary, then right-click in the query editor tab/window, select "Query Options..." and go to Results -> Text.
I don't remember what the default value for "Maximum number of characters displayed in each column" was, but if it's not 2097152, then I suggested making that change the new default (i.e. via the "Tools" menu).
If the code is truncated due to being more than 2,097,152 bytes (characters in most cases), then a) yikes!, and b) use the XML method described below.
Original Answer
If you don't mind an extra line at the very top and very bottom that should be ignored, you can use the following to display the full text, as text and not as a result set:
SELECT 1 AS [Tag], 0 AS [Parent], NCHAR(13) + NCHAR(10) +
OBJECT_DEFINITION(OBJECT_ID(N'sys.sp_helptext')) AS [Code!1!!CDATA]
FOR XML EXPLICIT;
It works the same across all versions starting with SQL Server 2005 (well, I assume from seeing that it worked on 2005, 2017, and 2019 CTP 2.2). I used a "CDATA" section (which, in turn, requires using the EXPLICIT
mode of FOR XML
) so that XML "special" characters <
, >
, &
, and "
would not be escaped as <
, >
, &
, and "
, respectively.
If you execute the T-SQL shown above, you will get back a one row, one column result set of an XML value. Click on the underlined XML value and it will open up in a new tab showing the full, properly formatted text (along with a first line of <Code><![CDATA[
and a final line of ]]></Code>
):
<Code><![CDATA[
create procedure sys.sp_helptext
@objname nvarchar(776)
,@columnname sysname = NULL
as
set nocount on
declare @dbname sysname
... {redacted for space} ...
select Text from #CommentText order by LineId
CLOSE ms_crs_syscom
DEALLOCATE ms_crs_syscom
DROP TABLE #CommentText
return (0) -- sp_helptext
]]></Code>
Depending on the size of the item you are "displaying", you might need to increase the SSMS setting for how much XML data it pulls down (else you might see truncated results). Go to:
Tools | Options | Query Results | SQL Server | Results to Grid
Then, under "Maximum Characters Retrieved", set "XML data" to (options are: 1 MB, 2 MB, 5 MB, and Unlimited). Then click the "OK" button.