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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create trigger or stored procedure in external DB?

Status
Not open for further replies.

raindogs

Programmer
Nov 23, 2005
27
US
I know you're not really supposed to do this, but I have a stored procedure that creates another database (using the tips in this thread: It all works great, except that I need to also add some triggers and/or stored procedures in the new DB I'm creating. SQL Server does not seem too happy about me using a stored procedure to create these elements in an external database. Is there a way out of this or have I painted myself into a corner?

Thanks,
Alex
 
You would need to use Dynamic SQL with a USE command within the dynamic SQL.
Code:
create procedure something as
declare @cmd varchar(8000)
CREATE DATABASE somethingelse
set @cmd = 'USE somethingelse
create procedure usp_someprocedure as
select * from sysobjects
go'
exec (@cmd)
go

Denny
MCSA (2003) / MCDBA (SQL 2000) / MCTS (SQL 2005) / MCITP Database Administrator (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top