Home All Groups Group Topic Archive Search About
Author
29 Jun 2006 11:24 AM
Robert Bravery
Hi all,

How can I select the contents of  a SP to a text file

Thanks
Robert

Author
29 Jun 2006 12:05 PM
Razvan Socol
Robert Bravery wrote:
> How can I select the contents of  a SP to a text file

In SQL Server 2005, you can use the OBJECT_DEFINITION function, like
this:
SELECT OBJECT_DEFINITION(OBJECT_ID('ProcedureName'))

In SQL Server 2000, you can query the syscomments table, like this:
SELECT text FROM syscomments WHERE id=OBJECT_ID('ProcedureName')
but if the procedure text is longer than 4K, it will be split across
multiple rows.

To store the result in a file, either use copy/paste (if it's a
one-time job) or a command-line utility like BCP or OSQL/SQLCMD.

Razvan
Author
29 Jun 2006 1:07 PM
Aaron Bertrand [SQL Server MVP]
> In SQL Server 2000, you can query the syscomments table, like this:
> SELECT text FROM syscomments WHERE id=OBJECT_ID('ProcedureName')
> but if the procedure text is longer than 4K, it will be split across
> multiple rows.

All the more reason to use sp_helptext instead of selecting from system
tables.
Author
30 Jun 2006 5:16 AM
Razvan Socol
Aaron Bertrand [SQL Server MVP] wrote:
> > In SQL Server 2000, you can query the syscomments table, like this:
> > SELECT text FROM syscomments WHERE id=OBJECT_ID('ProcedureName')
> > but if the procedure text is longer than 4K, it will be split across
> > multiple rows.
>
> All the more reason to use sp_helptext instead of selecting from system
> tables.

Of course; I forgot about sp_helptext.

Razvan
Author
29 Jun 2006 1:14 PM
SQL Menace
Another one way in SQL Server 2005 is by using the sys.sql_modules
catalog view

SELECT definition
FROM sys.sql_modules
WHERE object_id = OBJECT_ID('ProcedureName')

Denis the SQL Menace
http://sqlservercode.blogspot.com/

Razvan Socol wrote:
Show quote
> Robert Bravery wrote:
> > How can I select the contents of  a SP to a text file
>
> In SQL Server 2005, you can use the OBJECT_DEFINITION function, like
> this:
> SELECT OBJECT_DEFINITION(OBJECT_ID('ProcedureName'))
>
> In SQL Server 2000, you can query the syscomments table, like this:
> SELECT text FROM syscomments WHERE id=OBJECT_ID('ProcedureName')
> but if the procedure text is longer than 4K, it will be split across
> multiple rows.
>
> To store the result in a file, either use copy/paste (if it's a
> one-time job) or a command-line utility like BCP or OSQL/SQLCMD.
>
> Razvan
Author
29 Jun 2006 3:36 PM
Arnie Rowland
And of course there is always INFORMATION_SCHEMA.ROUTINES that works EQUALLY well in SQL 2000 and SQL 2005.

SELECT
     ROUTINE_NAME
   , ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_NAME = {MySprocName}

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


Show quote
"Razvan Socol" <rso***@gmail.com> wrote in message news:1151582710.965580.171600@d56g2000cwd.googlegroups.com...
> Robert Bravery wrote:
>> How can I select the contents of  a SP to a text file
>
> In SQL Server 2005, you can use the OBJECT_DEFINITION function, like
> this:
> SELECT OBJECT_DEFINITION(OBJECT_ID('ProcedureName'))
>
> In SQL Server 2000, you can query the syscomments table, like this:
> SELECT text FROM syscomments WHERE id=OBJECT_ID('ProcedureName')
> but if the procedure text is longer than 4K, it will be split across
> multiple rows.
>
> To store the result in a file, either use copy/paste (if it's a
> one-time job) or a command-line utility like BCP or OSQL/SQLCMD.
>
> Razvan
>
Author
29 Jun 2006 3:48 PM
SQL Menace
But if your proc is longer than 4000 characters then only the first
4000 character will be returned, the rest will be truncated

Denis the SQL Menace
http://sqlservercode.blogspot.com/

Arnie Rowland wrote:
Show quote
> And of course there is always INFORMATION_SCHEMA.ROUTINES that works EQUALLY well in SQL 2000 and SQL 2005.
>
> SELECT
>      ROUTINE_NAME
>    , ROUTINE_DEFINITION
> FROM INFORMATION_SCHEMA.ROUTINES
> WHERE ROUTINE_NAME = {MySprocName}
>
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
>
> *Yet Another Certification Exam
>
>
> "Razvan Socol" <rso***@gmail.com> wrote in message news:1151582710.965580.171600@d56g2000cwd.googlegroups.com...
> > Robert Bravery wrote:
> >> How can I select the contents of  a SP to a text file
> >
> > In SQL Server 2005, you can use the OBJECT_DEFINITION function, like
> > this:
> > SELECT OBJECT_DEFINITION(OBJECT_ID('ProcedureName'))
> >
> > In SQL Server 2000, you can query the syscomments table, like this:
> > SELECT text FROM syscomments WHERE id=OBJECT_ID('ProcedureName')
> > but if the procedure text is longer than 4K, it will be split across
> > multiple rows.
> >
> > To store the result in a file, either use copy/paste (if it's a
> > one-time job) or a command-line utility like BCP or OSQL/SQLCMD.
> >
> > Razvan
> >
> ------=_NextPart_000_0E97_01C69B57.2E222ED0
> Content-Type: text/html; charset=iso-8859-1
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 2519
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.5296.0" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY>
> <DIV><FONT face=Arial size=2>And of course there is always
> INFORMATION_SCHEMA.ROUTINES that works EQUALLY well in SQL 2000 and SQL
> 2005.</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face="Courier New" size=2>SELECT </FONT></DIV>
> <DIV><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;
> ROUTINE_NAME</FONT></DIV>
> <DIV><FONT face="Courier New" size=2>&nbsp;&nbsp; ,
> ROUTINE_DEFINITION</FONT></DIV>
> <DIV><FONT face="Courier New" size=2>FROM
> INFORMATION_SCHEMA.ROUTINES</FONT></DIV>
> <DIV><FONT face="Courier New" size=2>WHERE ROUTINE_NAME =
> {MySprocName}</FONT></DIV>
> <DIV><BR><FONT face=Arial size=2>-- <BR>Arnie Rowland, YACE* <BR>"To be
> successful, your heart must accompany your knowledge."</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>*Yet Another Certification Exam</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>"Razvan Socol" &lt;</FONT><A
> href="mailto:rso***@gmail.com"><FONT face=Arial
> size=2>rso***@gmail.com</FONT></A><FONT face=Arial size=2>&gt; wrote in message
> </FONT><A
> href="news:1151582710.965580.171***@d56g2000cwd.googlegroups.com"><FONT
> face=Arial
> size=2>news:1151582710.965580.171600@d56g2000cwd.googlegroups.com</FONT></A><FONT
> face=Arial size=2>...</FONT></DIV><FONT face=Arial size=2>&gt; Robert Bravery
> wrote:<BR>&gt;&gt; How can I select the contents of&nbsp; a SP to a text
> file<BR>&gt; <BR>&gt; In SQL Server 2005, you can use the OBJECT_DEFINITION
> function, like<BR>&gt; this:<BR>&gt; SELECT
> OBJECT_DEFINITION(OBJECT_ID('ProcedureName'))<BR>&gt; <BR>&gt; In SQL Server
> 2000, you can query the syscomments table, like this:<BR>&gt; SELECT text FROM
> syscomments WHERE id=OBJECT_ID('ProcedureName')<BR>&gt; but if the procedure
> text is longer than 4K, it will be split across<BR>&gt; multiple rows.<BR>&gt;
> <BR>&gt; To store the result in a file, either use copy/paste (if it's a<BR>&gt;
> one-time job) or a command-line utility like BCP or OSQL/SQLCMD.<BR>&gt;
> <BR>&gt; Razvan<BR>&gt;</FONT></BODY></HTML>
>
> ------=_NextPart_000_0E97_01C69B57.2E222ED0--
Author
29 Jun 2006 4:02 PM
Arnie Rowland
Quite true. I should have added that as a 'proviso'. (My rule of thumb is
that if the sproc exceeds 4k chars, then it is probably not very ATOMIC and
most likely is a candidate for re-enginering. -it doesn't always work, but
is good to have as a goal.)

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


Show quote
"SQL Menace" <denis.g***@gmail.com> wrote in message
news:1151596104.047640.163330@j72g2000cwa.googlegroups.com...
> But if your proc is longer than 4000 characters then only the first
> 4000 character will be returned, the rest will be truncated
>
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
>
> Arnie Rowland wrote:
>> And of course there is always INFORMATION_SCHEMA.ROUTINES that works
>> EQUALLY well in SQL 2000 and SQL 2005.
>>
>> SELECT
>>      ROUTINE_NAME
>>    , ROUTINE_DEFINITION
>> FROM INFORMATION_SCHEMA.ROUTINES
>> WHERE ROUTINE_NAME = {MySprocName}
>>
>> --
>> Arnie Rowland, YACE*
>> "To be successful, your heart must accompany your knowledge."
>>
>> *Yet Another Certification Exam
>>
>>
>> "Razvan Socol" <rso***@gmail.com> wrote in message
>> news:1151582710.965580.171600@d56g2000cwd.googlegroups.com...
>> > Robert Bravery wrote:
>> >> How can I select the contents of  a SP to a text file
>> >
>> > In SQL Server 2005, you can use the OBJECT_DEFINITION function, like
>> > this:
>> > SELECT OBJECT_DEFINITION(OBJECT_ID('ProcedureName'))
>> >
>> > In SQL Server 2000, you can query the syscomments table, like this:
>> > SELECT text FROM syscomments WHERE id=OBJECT_ID('ProcedureName')
>> > but if the procedure text is longer than 4K, it will be split across
>> > multiple rows.
>> >
>> > To store the result in a file, either use copy/paste (if it's a
>> > one-time job) or a command-line utility like BCP or OSQL/SQLCMD.
>> >
>> > Razvan
>> >
>> ------=_NextPart_000_0E97_01C69B57.2E222ED0
>> Content-Type: text/html; charset=iso-8859-1
>> Content-Transfer-Encoding: quoted-printable
>> X-Google-AttachSize: 2519
>>
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
>> <HTML><HEAD>
>> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
>> <META content="MSHTML 6.00.5296.0" name=GENERATOR>
>> <STYLE></STYLE>
>> </HEAD>
>> <BODY>
>> <DIV><FONT face=Arial size=2>And of course there is always
>> INFORMATION_SCHEMA.ROUTINES that works EQUALLY well in SQL 2000 and SQL
>> 2005.</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face="Courier New" size=2>SELECT </FONT></DIV>
>> <DIV><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;
>> ROUTINE_NAME</FONT></DIV>
>> <DIV><FONT face="Courier New" size=2>&nbsp;&nbsp; ,
>> ROUTINE_DEFINITION</FONT></DIV>
>> <DIV><FONT face="Courier New" size=2>FROM
>> INFORMATION_SCHEMA.ROUTINES</FONT></DIV>
>> <DIV><FONT face="Courier New" size=2>WHERE ROUTINE_NAME =
>> {MySprocName}</FONT></DIV>
>> <DIV><BR><FONT face=Arial size=2>-- <BR>Arnie Rowland, YACE* <BR>"To be
>> successful, your heart must accompany your knowledge."</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>*Yet Another Certification Exam</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>"Razvan Socol" &lt;</FONT><A
>> href="mailto:rso***@gmail.com"><FONT face=Arial
>> size=2>rso***@gmail.com</FONT></A><FONT face=Arial size=2>&gt; wrote in
>> message
>> </FONT><A
>> href="news:1151582710.965580.171***@d56g2000cwd.googlegroups.com"><FONT
>> face=Arial
>> size=2>news:1151582710.965580.171600@d56g2000cwd.googlegroups.com</FONT></A><FONT
>> face=Arial size=2>...</FONT></DIV><FONT face=Arial size=2>&gt; Robert
>> Bravery
>> wrote:<BR>&gt;&gt; How can I select the contents of&nbsp; a SP to a text
>> file<BR>&gt; <BR>&gt; In SQL Server 2005, you can use the
>> OBJECT_DEFINITION
>> function, like<BR>&gt; this:<BR>&gt; SELECT
>> OBJECT_DEFINITION(OBJECT_ID('ProcedureName'))<BR>&gt; <BR>&gt; In SQL
>> Server
>> 2000, you can query the syscomments table, like this:<BR>&gt; SELECT text
>> FROM
>> syscomments WHERE id=OBJECT_ID('ProcedureName')<BR>&gt; but if the
>> procedure
>> text is longer than 4K, it will be split across<BR>&gt; multiple
>> rows.<BR>&gt;
>> <BR>&gt; To store the result in a file, either use copy/paste (if it's
>> a<BR>&gt;
>> one-time job) or a command-line utility like BCP or OSQL/SQLCMD.<BR>&gt;
>> <BR>&gt; Razvan<BR>&gt;</FONT></BODY></HTML>
>>
>> ------=_NextPart_000_0E97_01C69B57.2E222ED0--
>
Author
29 Jun 2006 4:07 PM
Aaron Bertrand [SQL Server MVP]
> Quite true. I should have added that as a 'proviso'. (My rule of thumb is
> that if the sproc exceeds 4k chars, then it is probably not very ATOMIC
> and most likely is a candidate for re-enginering. -it doesn't always work,
> but is good to have as a goal.)

I'd agree.  I very rarely see procedures that exceed 2k, except when I am
working on them for other reasons than size (e.g. they are slow, or do
stupid things).  I have inherited a few doozies in the past but there are
certainly none that large in any of the systems I currently maintain (never
mind develop).

A

AddThis Social Bookmark Button