Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying a stored procedure to multiple databases

Status
Not open for further replies.

DanC

Programmer
Jan 12, 2001
65
US
I'm trying to copy a stored procedure to several client databases using the sp_MSforeachdb but am having some trouble.

the command i'm trying to run is:

EXEC sp_MSforeachdb @command1 = 'Use ? ; if not exists (select [name] from dbo.sysobjects where id = object_id(N'[dbo].[mystoredprocedure]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
create procedure mystoredprocedure as
select * from mytable

or something like that. I keep getting an incorrect syntax near create error. Can anyone tell me what I'm doing wrong?
 
try this:

EXEC sp_MSforeachdb @command1 = 'Use ? if not exists (select [name] from dbo.sysobjects where id = object_id(N''[dbo].[mystoredprocedure]'') and OBJECTPROPERTY(id, N''IsProcedure'') = 1)
BEGIN
declare @temp varchar(500)
select @temp = ''create procedure mystoredprocedure as
select * from mytable''
exec (@temp)
END
'


cheyney
 
thanks a lot, that solved the problem
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top