SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Export a list of Stored Procedures


Export a list of Stored Procedures

Author
Message
GarethPhilpott
GarethPhilpott
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 48
Ok this is driving me nuts Crazy

The problem:
I need to extract out of the database all of the stored procedures that are about to be updated. I know what they are as I have a directory full of SQL files named for the procs.

So far I have been using the manual right click and export, but that is time consuming and error prone (no errors yet but you never know).

My solutions (so far):
I have so far tried to extract them using a batch file with a for loop that goes through the directory and collects all of the file names (the names will ALWAYS reflect the stored procedure in the file, no exceptions) then runs bcp.
Like so: bcp "Select routine_definition from %1.information_Schema.routines WHERE routine_name = '%%~nc'" queryout "%varbackup%\%%c" -c -T

That worked a treat until I looked closer at the files it output and saw that a couple of the bigger procs were only partially exported. Looking closer it was only exporting the first 4000 chars, this was due to the way information_Schema.routines works Blush. I should have read that part a little closer.

So I tried:
bcp "SELECT Definition FROM sys.sql_modules WHERE object_id = OBJECT_ID('%%~nc')" queryout "%varbackup%\%%c" -c -T

Cool the SQL works in EM so it'll be fine, nope bcp produces an empty file Angry

My reasoning behind the batch is as crude as the batch is, it's quick. Quick to edit if a change is required and quick to run during a frantic upgrade at some horrible time of the morning when no one should be awake.

So the crux of my post:
What SQL should I be running to make it spit out the procs?
Is this the best way to do it?
george sibbald
george sibbald
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24234 Visits: 13698
check out sp_helptext

---------------------------------------------------------------------
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15499 Visits: 25280
GarethPhilpott
So the crux of my post:
What SQL should I be running to make it spit out the procs?


Try this ... tested using SSMS -- with query output as text in new tab.

Select ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_DEFINITION  
From INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE='PROCEDURE'
AND ROUTINE_DEFINITION LIKE '%update%'



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
GarethPhilpott
GarethPhilpott
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 48
Thanks for the answers guys, unfortunately niether of them did the trick Sad. I've decided just to add to a C# app that I've already written and use the SMO.

george sibbald
sp_helptext doesn't work with BCP properly, I found a couple of ways of doing it but they were far more complex than using SMO.

bitbucket-25253
Yup I had already tried that in SSMS (Thats how I constructed the query in the first place) and it pumped out the whole sp, BCP has a character limit for which there does not appear to be a switch to override.
george sibbald
george sibbald
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24234 Visits: 13698
why use bcp at all? Whats wrong with a SQLAgent job that outputs to a text file?

That sounds easier and quicker than a C# routine and SMO :-)

---------------------------------------------------------------------
Carlo Mitchell
Carlo Mitchell
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 37
I'm not sure if this would apply to your situation but, you might consider taking a different approach and setting up a database trigger which logs any procedure updates to a table:

CREATE trigger [dbtrg_ChangeLog]
on database
for create_procedure, alter_procedure, drop_procedure
as

set nocount on

declare @data xml
set @data = EVENTDATA()

INSERT INTO changelog(databasename, eventtype, objectname, objecttype, sqlcommand, loginname)
VALUES(
@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)'),
@data.value('(/EVENT_INSTANCE/EventType)[1]', 'varchar(50)'),
@data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
@data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(25)'),
@data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)'),
@data.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)')
)
GarethPhilpott
GarethPhilpott
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 48
george sibbald
Yeah an agent job could have worked, oh well it only took me a couple of hours to write the SMO code.

Having said that though the SMO solution is less than 40 lines long and that includes error handling, comments and a lot of whitespace :-)

Carlo Mitchell
Yeah I could do that, but BCP was the peice that was failing. The SQL I was running was returning the procedure correctly so there was no need to replicate something that SQL Server already does. I'm also not a fan of storing them that way as all it takes is the trigger to fail once at a critical time and the table is one step behind what the database has as the latest procedure or worse it's mangled.
corey lawson
corey lawson
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 575
Hmm... this is working for me...

c:>bcp "select text from sys.syscomments where id=OBJECT_ID('procname')" QUERYOUT "procname.sql" -S server -d database -T

Even better, if I create a text file of proc (or view, function or trigger) names:

c:>for /f %f in (proclist.txt) do @bcp "select text from sys.syscomments where id=OBJECT_ID('%f')" QUERYOUT "%f.sql" -S server -d database -T

If using a version of bcp that doesn't support -d database, then modify the query to:
select text from database.sys.comments

Wish there was a bit easier way to extract table DDL (yes, there's SMO)...
corey lawson
corey lawson
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 575
Ooops... my bcp commands should include the -c option...

C:>for /f %f in (proclist.txt) do @bcp "select text
from sys.syscomments where id=OBJECT_ID('%f')" queryout "%f.txt" -S server
-d database -T -c
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search