Ability to export the sql behind a SP / view / query to an ascii file ?

Posted on 2004-03-23
Last Modified: 2012-05-04
Is there a way that I can export the SQL behind a stored procedure or view, into a text file, or preferably into a recordset string field.

My absolute ideal would to have a stored procedure that returns a recordset with the database object name and the SQL in the object, be it a stored procedure or a view, that I can use to get ascii copies of all my db objects, such that I can see their progress over time in CVS.

Any ideas ?

Question by:mj_cole
  • 3
LVL 34

Assisted Solution

arbert earned 250 total points
ID: 10663811
sure, if you want it in a recordset, just execute the sp_helptext stored proc with the stored procedure you want the text of:

rs=conn.execute("sp_helptext mystoredproc")

You can also right click on your database (in enterprise manager) and generate SQL Scripts for your entire database or certain objects.

LVL 34

Expert Comment

ID: 10663818
You can also write queries against the information.schema_views (located in the master database) to retrieve information....


Author Comment

ID: 10664298
sp_helptext  seems to do the job :)

is there a master SP to return all the stored procedures and views, such that I can then write my own SP to get the list and then call sp_helptext on that list ?


LVL 34

Accepted Solution

arbert earned 250 total points
ID: 10664412
Nope, but that's where the information_schema.views come in.....

You can run this from your database and it will list the procs and views for you:

select 'proc', routine_name from information_schema.routines
select 'View',view_name from information_schema.view_table_usage

However, using the information schema views, you can also return the text of the views and procs and you don't have to use sp_helptext.

LVL 69

Expert Comment

by:Scott Pletcher
ID: 10669100
Actually I think the information_schema views only return the first 4K of object text, so it's not a safe way to get the source.  You really need to use sp_helptext.

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now