Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:17:09+00:00 2026-06-10T13:17:09+00:00

This is a question related to the context when calling a stored procedure from

  • 0

This is a question related to the context when calling a stored procedure from one database in the context of another database.

Say I have a procedure created in the MainDB:

USE MainDB;
GO

CREATE PROCEDURE dbo.sp_mainproc
    @Login   nvarchar(50),
    @Error   INT               OUTPUT
AS
BEGIN
    -- many details left out...

    -- Login as string must be captured in the xUser table to get
    -- the personal settings for the user...

    SET @_user_id = ( SELECT dbo.xUser.user_id
                      FROM dbo.xUser
                      WHERE dbo.xUser.login = @Login );

    IF( @_user_id IS NULL )
    BEGIN
        -- The user with the given @Login is not present. Indicate the failure.
        SET @Error = 2
        RETURN (1)
    END

    -- Do something in the MainDB. Here the main reason for calling
    -- the stored procedure is implemented.

    -- Indicate the success when finishing.
    SET @Error = 0
    RETURN (0)
END
GO

Now, I want to call the procedure from another procedure in the AuxDB:

USE AuxDB;
GO

CREATE PROCEDURE dbo.sp_action
AS
BEGIN
    -- Call the MainDB.dbo.sp_mainproc to do the action in the MainDB.
    -- The login name must be passed, and possible error must be checked.

    DECLARE @error   INT
    DECLARE @retcode INT

    EXEC @retcode = MainDB.dbo.sp_mainproc
                                  N'the_user',
                                  @error OUTPUT

    IF (@retcode <> 0)
    BEGIN
        -- Here the error must be signalized.
        RETURN 1
    END

    -- Everything OK, let's continue...

    RETURN 0
END
GO

My question is: When the MainDB.dbo.sp_mainproc is called from within AuxDB.dbo.sp_action, where the dbo.xUser table used in the sp_mainproc is searched for. Is the MainDB.dbo.xUser considered, or is the AuxDB.dbo.xUser searched for?

Thanks,
Petr

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:17:10+00:00Added an answer on June 10, 2026 at 1:17 pm

    Procs are compiled, so it will refer to the object in the same database in which the dbo.sp_mainproc exists, because when the proc was created, it refers only to dbo.xUser, which doesn’t have a database name part

    (i.e. MainDB.dbo.sp_mainproc will use MainDB.dbo.xUser irrespective of which database that the proc is called from).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question is related to database/RDBMS where i have little knowledge regarding performance and
I have asked another question related to this in this thread Where to put
This question is related to the one I asked here . I'm trying to
This question is related to another question, where I wanted to define a custom
This question is related to awk, I suppose. I have no experience with awk.
This question is related to another question of mine. Thanks to some help I
This question is related somewhat to the one i asked HERE . Now, i
This question is related to this one . I managed to modify my code
Background/context for this question: I have a WPF desktop application. It uses LINQ to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.