Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


create Procedure within Procedure


create Procedure within Procedure

Author
Message
Trouble Shooter
Trouble Shooter
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 74
Hi,
I want to create procedure within procedure

something like that




create procedure aa
as
begin

if exists (select * from sys.objects where name='bb')
begin

drop procedure bb
go
declare @a nvarchar(max)
set @a='create procedure bb as begin print ''bb'' end'
end

end






Thanks in Advance
GilaMonster
GilaMonster
SSC Guru
SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)

Group: General Forum Members
Points: 54298 Visits: 44637
Is there a question here? Your code looks like it should work, just add an EXEC to run the dynamic sQL you generated.

I do have to ask... Why?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Trouble Shooter
Trouble Shooter
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 74
Hi,
Thanks for reply



create procedure aa
as
begin

if exists (select * from sys.objects where name='bb')
begin

drop procedure bb
go
declare @a nvarchar(max)
set @a='create procedure bb as begin print ''bb'' end'
exec sp_executesql @a

end

end



after run this code i got following error message



Msg 102, Level 15, State 1, Procedure aa, Line 9
Incorrect syntax near 'bb'.
Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'end'.



GilaMonster
GilaMonster
SSC Guru
SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)

Group: General Forum Members
Points: 54298 Visits: 44637
Well, the GO in the 'middle' of procedure A ends the batch. Hence leaving a begin without an end and, in the second batch an end without a begin.

GO is a batch breaker. It tells SSMS where to delimit the batches sent to SQL. So what you're telling SSMS is that the first batch it must send is

create procedure aa
as
begin

if exists (select * from sys.objects where name='bb')
begin

drop procedure bb
GO



As I'm sure you can see, that is not valid SQL.

Again, I need to ask why you want a procedure to create a procedure? What's the point?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Ramji29
Ramji29
SSC Veteran
SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)

Group: General Forum Members
Points: 249 Visits: 479
or it shud be like below

create procedure aa
as
begin

if exists (select * from sys.objects where name='bb')
begin

drop procedure bb
declare @a nvarchar(max)
set @a='create procedure bb as begin print ''bb'' end'
exec sp_executesql @a

end

end

But as Gail's suggestion.... why u want to do this??? is this some class room based question???
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