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 2005 Forums
 .NET Inside SQL Server (2005)
 Master SPROC used on different DBs

Author  Topic 

Jster
Starting Member

21 Posts

Posted - 2007-10-30 : 11:26:25
I'm trying to create a stored procedure on my master database that I can query other databases. The reason behind putting it on the master db is so I will have one place to modify the stored procedure instead of potentially a hundred different databases. I am trying to create a stored procedure like the one below, but it seems to not like it and I get the error below. Any suggestions are appreciated.

DECLARE @uName AS nvarchar(30), @dbName as nvarchar(5)
SET @uName='TU'
SET @dbName = '00000'
DELETE FROM [DASHDB].[dbo].users WHERE Username=@uName
EXECUTE('USE ['+@dbName+']')
DELETE FROM DashboardBranches WHERE Username=@uName
DELETE FROM MyDashboardSettings WHERE Username=@uName
DELETE FROM MyFavorites WHERE Username=@uName
DELETE FROM MyModules WHERE Username=@uName
DELETE FROM MyStocks WHERE Username=@uName
DELETE FROM BoardPackage WHERE PackageID IN (SELECT ID FROM BPackages WHERE Username=@uName)
DELETE FROM BPackages WHERE Username=@uName



ERROR:
Msg 208, Level 16, State 1, Line 6
Invalid object name 'DashboardBranches'.


Thanks,
Josh

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2007-10-30 : 13:02:15
should look at sp_executesql in BOL.

else can make the whole script dynamic sql (cant just say use db) like u r trying.

if you are keeping 100 copies of same data in synch, i would look at transactional replication...
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2007-10-30 : 13:05:32
or can even replicate sp, no dynamic sql at all in it
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-30 : 13:24:50
maybe this will be of some help:
http://weblogs.sqlteam.com/mladenp/archive/2007/01/18/58287.aspx

i'm not saying it'sa good idea though

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

Jster
Starting Member

21 Posts

Posted - 2007-10-30 : 14:26:49
Thank you spirit1, that helped me come up with a solution.
Go to Top of Page
   

- Advertisement -