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


Cursor to generate Stored procedures Script


Cursor to generate Stored procedures Script

Author
Message
Minnu
Minnu
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1371 Visits: 950
Hi Team,

is it possible to get all stored procedures script using a cursor.

Can u please provide the query
Prassad Dabbada V R
Prassad Dabbada V R
Mr or Mrs. 500
Mr or Mrs. 500 (514 reputation)Mr or Mrs. 500 (514 reputation)Mr or Mrs. 500 (514 reputation)Mr or Mrs. 500 (514 reputation)Mr or Mrs. 500 (514 reputation)Mr or Mrs. 500 (514 reputation)Mr or Mrs. 500 (514 reputation)Mr or Mrs. 500 (514 reputation)

Group: General Forum Members
Points: 514 Visits: 173
I am using temp table instead of cursor.

declare @spname varchar(100), @sptext varchar(max), @cnt int, @i int
select @spname = '', @sptext = '', @cnt = 0, @i = 1;

create table #spnames(id int identity(1,1), spname varchar(100))

insert into #spnames(spname)
select distinct schema_name(schema_id) + '.' + name from sys.objects where type = 'P'

select @cnt = count(1) from #spnames

while (@i <= @cnt)
begin
select @spname = spname from #spnames where id = @id
select @sptext = object_definition(object_id) from sys.objects where = schema_name(schema_id) + '.' + name = @spname
print @sptext
set @i = @i+i
end
GO
drop table #spnames
sharky
sharky
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
Points: 218 Visits: 441
You might also try this with a TVF.


CREATE FUNCTION dbo.FnGetDefinition(@ObjId INT)
RETURNS @DefTable TABLE(ObjectDef VARCHAR(MAX))
AS
BEGIN
INSERT INTO @DefTable SELECT object_definition(@ObjID)
RETURN
END;
GO
SELECT P.name,D.ObjectDef FROM sys.procedures As P
CROSS APPLY dbo.FnGetDefinition(p.object_id) As D


anthony.green
anthony.green
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: 24696 Visits: 6519
Or just query the sys.sql_modules table


select
o.name,
sm.definition
from
sys.objects o
inner join
sys.sql_modules sm
on
o.object_id = sm.object_id
where
o.type = 'p'





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Lowell
Lowell
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72582 Visits: 40946
I would basically do it the same as anthony; they are already in a table, so not temp table or cursor required.

I kind of like some of the built in functions, so I'd use those for schema/object name:

select
OBJECT_SCHEMA_NAME(object_id) AS SchemaName,
OBJECT_NAME(object_id) AS ObjectName,
definition AS ObjectDefinition
FROM sys.sql_modules



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
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