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
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Creating a view in a different database

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-04-20 : 10:01:01
Bruce writes "This doesn't seem like it should be difficult, but I just can't find anything describing how to do it correctly.

I have a stored procedure in the master database which I would like to use to create a view in a different database. Since the "Create View" syntax does not allow the specification of a database, I guess I need to do it dynamically, but cant get it to work.

An example of what I'd like to do is -

Create Procedure create_view @dbname sysname
AS
BEGIN
DECLARE @query varchar(100)
SELECT @query = 'use ' + @dbname + ' go CREATE VIEW.....'
EXEC(@query)
END

This example gives a "CREATE VIEW" must be the first statement in a batch" error. Replacing GO with a semicolon didn't help much either.

Any help would be GREATLY appreciated.

Thanks,
Bruce"

MuadDBA

628 Posts

Posted - 2004-04-20 : 10:24:10
the procedur emight be in the master database, but you shouldn't be executing it while connected to the MASTER database....connect to the database you want to build the view in, and then do exec master..create_view

That should solve your problem.
Go to Top of Page
   

- Advertisement -