Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating a stored procedure in another database

Author  Topic 

issam
Starting Member

30 Posts

Posted - 2008-02-27 : 04:56:03
From within a stored procedure in an existing database I'm creating a new database from the scratch.

In the query window if I write the statement:
CREATE DATABASE [tomasdb]

USE [tomasdb]
GO
CREATE PROCEDURE TEST1
AS
BEGIN
DECLARE @something varchar(12)
set @something = '123'
END

everything works ok and the database is created and the stored procedure Test1 is created within it.
However, if I want to do the same from another stored procedure (in another database), i.e. I write

EXEC('
CREATE DATABASE [tomasdb]
USE [tomasdb]
CREATE PROCEDURE TEST1
AS
BEGIN
DECLARE @something varchar(12)
set @something = ''123''
END
')

the problem I get is that I'm not allowed to put USE to change the execution context into [tomasdb] before the CREATE PROCEDURE statement.
My question is: how can I change the context in this situation / or / can I create the stored procedure in the [tomasdb] database somehow from outside (another context).

Thanks,
Tomas

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-27 : 05:02:54
I dont think you can change context of db by executing USE statement from within a db using EXEC
Go to Top of Page

issam
Starting Member

30 Posts

Posted - 2008-02-27 : 05:11:15
what about copying a stored procedure from one database to another ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-27 : 05:34:07
You can copy it using transfer objects task in SSIS
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-27 : 05:38:27
quote:
Originally posted by issam

what about copying a stored procedure from one database to another ?


Scipt out at source db and run it in target db

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -