Stored procedure, which table is used ?

From: chris (anonymous_at_discussions.microsoft.com)
Date: 02/19/04


Date: Thu, 19 Feb 2004 07:58:04 -0800


>Which table does a stored procedure use when
>run in the Query analyser under local database.

Whichever table the proc calls.

I may not be following here. All your queries and all your
procs will run in the local db by default. The exception
to this would proably be some of the "sp_"'s which will
probably be run in Master even if called from a user db.

>-----Original Message-----
>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)
  • Stored procedure, which table is used ?
    ... A stored procedure in the master database. ... select 'LocalDatabase' as OKKI INTO DEMOTABLE ...
    (microsoft.public.sqlserver.server)
  • Re: Stored procedure, which table is used ?
    ... The first select takes the table from Master. ... The second select (in the exec) takes the table from the ... >>run in the Query analyser under local database. ... >>use LocalDatabase ...
    (microsoft.public.sqlserver.server)
  • Re: Stored procedure, which table is used ?
    ... > risk etc etc. ... Is there a reasoning behind the local versus master table use? ... >> run in the Query analyser under local database. ... >> use LocalDatabase ...
    (microsoft.public.sqlserver.server)
  • Re: osql
    ... State 62, Server NXP353467\ORDERS, Line 4 ... Could not find stored procedure 'sp_add_jobstep'. ... >For the backup error, you can't do a log backup if a ... >> USE master ...
    (microsoft.public.sqlserver.tools)