Stored procedure, which table is used ?

From: ben brugman (ben_at_niethier.nl)
Date: 02/19/04


Date: Thu, 19 Feb 2004 15:19:51 +0100

A stored procedure in the master database.

Which table does a stored procedure use when
run in the Query analyser under local database.
If I do a 'normal' select from, it tries to read the
table form master.
During an exec the table comes from local database.

Why the difference ?

What should I do to 'force' the 'local' table from database ?

Using an exec around all my queries is less elegant.
(more confusing and error prone).
Is there a design that I am missing ?

ben brugman

RESULTS FROM HERE :

OKKI
------
Master

(1 row(s) affected)

OKKI
----------
LocalDatabase

THE DEMO CODE :

-- In the master database.
use master
go
CREATE PROCEDURE sp_demo AS
select * from demotable
exec ('select * from demotable')
GO

-- In the master database.
use master
select 'Master' as OKKI INTO DEMOTABLE

-- In another database.
use LocalDatabase
select 'LocalDatabase' as OKKI INTO DEMOTABLE

--In the query analyser run the sp_demo under
--the another database.

--
--
print 'RESULTS FROM HERE :'
use LocalDatabase
exec sp_demo
use LocalDatabase
drop table demotable
use master
drop table demotable
drop procedure sp_demo


Relevant Pages

  • Re: Stored procedure, which table is used ?
    ... I believe that you can mark a stored procedure as a "proper" system stored ... > A stored procedure in the master database. ... > run in the Query analyser under local database. ... > use LocalDatabase ...
    (microsoft.public.sqlserver.server)
  • Re: Extended Stored Procedure: Get the current db of the client
    ... the database context as a parameter if you need it, ... Did you ever write an extended stored procedure? ... Master, that's what. ... database you own in order to get the current database context out of it. ...
    (microsoft.public.sqlserver.odbc)
  • Re: Extended Stored Procedure: Get the current db of the client
    ... pick up the database context, but that does not work for an Extended Stored ... you would have to create a Stored Procedure ... Master, that's what. ...
    (microsoft.public.sqlserver.odbc)
  • Re: Replication problems
    ... An error occurred while accessing the DHCP database. ... DHCP server event log for more information on this error. ... browser for the domain on transport NetBT_Tcpip_{99B638B5-2C0B. ... master browser is stopping or an election is being forced. ...
    (microsoft.public.windows.server.active_directory)
  • RE: SQL Server failing to start (directly effecting sales!!!) Error: 823, Severity:
    ... You should consider calling Microsoft Technical Support as your master ... They will want your SQL Server Errorlogs ... that the machine with the database has hardware that is failing. ... Even if you are planning to reinstall you should make a backup copy ...
    (microsoft.public.sqlserver.msde)