I want to separate a specific set of stored procedures into a seperate database … sort of like a library. However, when I run these procs from another database, they appear to call objects from within the library database.
Here is some source code which demonstrates the problem
use mylibdb
go
create proc gettablecount
as
begin
declare @cnt int;
select @cnt=count(*)
from sysobjects
where xtype='U';
print 'Table count : ' + cast( @cnt as nvarchar);
end
go
use adventureworks
go
exec mylibdb.dbo.gettablecount;
print '';
select count(*) as [table count]
from sysobjects
where xtype='U';
Running that code will print out
Table count : 0
table count
71
(1 row(s) affected)
Notice the proc queries the mylibdb.dbo.sysobjects table, not adventureworks.dbo.sysobjects.
Does anybody know how I can do this without dynamic sql?