Can a stored procedure reference the database in which it is stored? Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?“Procedure: XXXX has an unresolved reference to object XXXX” errorsPassing array parameters to a stored procedureUsing the correct database when calling a system stored procedure in SQL Server 2008SQL command in stored procedure continue on errorHow to remove the WITH ENCRYPTION from the code of the procedure - via T-SQLCan I run a CLR Stored Procedure on a different server than the database instance?How can I see what called a stored procedure?How to change the schema of stored procedure without recreating itStored procedure body missingHow to get the name of the database a stored procedure is executed in within that stored procedure while it's executing?

Which big number is bigger?

A faster way to compute the largest prime factor

Map material from china not allowed to leave the country

What is purpose of DB Browser(dbbrowser.aspx) under admin tool?

What makes accurate emulation of old systems a difficult task?

Is it acceptable to use working hours to read general interest books?

Sharepoint Designer Discontinuation - software to modify existing workflows

Why does Arg'[1. + I] return -0.5?

Bayes factor vs P value

A strange hotel

Drawing a german abacus as in the books of Adam Ries

What *exactly* is electrical current, voltage, and resistance?

Could moose/elk survive in the Amazon forest?

Putting Ant-Man on house arrest

How do I reattach a shelf to the wall when it ripped out of the wall?

Is it possible to cast 2x Final Payment while sacrificing just one creature?

Intern got a job offer for same salary than a long term team member

What was Apollo 13's "Little Jolt" after MECO?

Negative Resistance

Contradiction proof for inequality of P and NP?

Is this homebrew arcane communication device abusable?

Can a stored procedure reference the database in which it is stored?

Crossed out red box fitting tightly around image

Older movie/show about humans on derelict alien warship which refuels by passing through a star



Can a stored procedure reference the database in which it is stored?



Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?“Procedure: XXXX has an unresolved reference to object XXXX” errorsPassing array parameters to a stored procedureUsing the correct database when calling a system stored procedure in SQL Server 2008SQL command in stored procedure continue on errorHow to remove the WITH ENCRYPTION from the code of the procedure - via T-SQLCan I run a CLR Stored Procedure on a different server than the database instance?How can I see what called a stored procedure?How to change the schema of stored procedure without recreating itStored procedure body missingHow to get the name of the database a stored procedure is executed in within that stored procedure while it's executing?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















Suppose I have a stored procedure that is duplicated, with some modifications, in several databases. And I want to reference the database in which the stored procedure is stored, even if it is executed in another database.



Is there a way to retrieve the full path (..) or otherwise retrieve the database in which the stored procedure is stored, rather than the current database?










share|improve this question







New contributor




Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    If it is duplicated in several databases, then it is stored in several databases, so what would be the (seemingly) single path you are looking for? Or am I misunderstanding your question?

    – scsimon
    3 hours ago












  • Are you calling the stored procedure in database A, from database B?

    – Anthony Genovese
    2 hours ago











  • Can you explain why you need to retrieve the full path and what you will do with it? The procedure executes in the database context where it lives, not the database context of its caller. Please show how this is failing you and how you think the full path could fix it.

    – Aaron Bertrand
    32 mins ago


















2















Suppose I have a stored procedure that is duplicated, with some modifications, in several databases. And I want to reference the database in which the stored procedure is stored, even if it is executed in another database.



Is there a way to retrieve the full path (..) or otherwise retrieve the database in which the stored procedure is stored, rather than the current database?










share|improve this question







New contributor




Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    If it is duplicated in several databases, then it is stored in several databases, so what would be the (seemingly) single path you are looking for? Or am I misunderstanding your question?

    – scsimon
    3 hours ago












  • Are you calling the stored procedure in database A, from database B?

    – Anthony Genovese
    2 hours ago











  • Can you explain why you need to retrieve the full path and what you will do with it? The procedure executes in the database context where it lives, not the database context of its caller. Please show how this is failing you and how you think the full path could fix it.

    – Aaron Bertrand
    32 mins ago














2












2








2








Suppose I have a stored procedure that is duplicated, with some modifications, in several databases. And I want to reference the database in which the stored procedure is stored, even if it is executed in another database.



Is there a way to retrieve the full path (..) or otherwise retrieve the database in which the stored procedure is stored, rather than the current database?










share|improve this question







New contributor




Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Suppose I have a stored procedure that is duplicated, with some modifications, in several databases. And I want to reference the database in which the stored procedure is stored, even if it is executed in another database.



Is there a way to retrieve the full path (..) or otherwise retrieve the database in which the stored procedure is stored, rather than the current database?







sql-server






share|improve this question







New contributor




Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Jim ClarkJim Clark

1112




1112




New contributor




Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jim Clark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2





    If it is duplicated in several databases, then it is stored in several databases, so what would be the (seemingly) single path you are looking for? Or am I misunderstanding your question?

    – scsimon
    3 hours ago












  • Are you calling the stored procedure in database A, from database B?

    – Anthony Genovese
    2 hours ago











  • Can you explain why you need to retrieve the full path and what you will do with it? The procedure executes in the database context where it lives, not the database context of its caller. Please show how this is failing you and how you think the full path could fix it.

    – Aaron Bertrand
    32 mins ago













  • 2





    If it is duplicated in several databases, then it is stored in several databases, so what would be the (seemingly) single path you are looking for? Or am I misunderstanding your question?

    – scsimon
    3 hours ago












  • Are you calling the stored procedure in database A, from database B?

    – Anthony Genovese
    2 hours ago











  • Can you explain why you need to retrieve the full path and what you will do with it? The procedure executes in the database context where it lives, not the database context of its caller. Please show how this is failing you and how you think the full path could fix it.

    – Aaron Bertrand
    32 mins ago








2




2





If it is duplicated in several databases, then it is stored in several databases, so what would be the (seemingly) single path you are looking for? Or am I misunderstanding your question?

– scsimon
3 hours ago






If it is duplicated in several databases, then it is stored in several databases, so what would be the (seemingly) single path you are looking for? Or am I misunderstanding your question?

– scsimon
3 hours ago














Are you calling the stored procedure in database A, from database B?

– Anthony Genovese
2 hours ago





Are you calling the stored procedure in database A, from database B?

– Anthony Genovese
2 hours ago













Can you explain why you need to retrieve the full path and what you will do with it? The procedure executes in the database context where it lives, not the database context of its caller. Please show how this is failing you and how you think the full path could fix it.

– Aaron Bertrand
32 mins ago






Can you explain why you need to retrieve the full path and what you will do with it? The procedure executes in the database context where it lives, not the database context of its caller. Please show how this is failing you and how you think the full path could fix it.

– Aaron Bertrand
32 mins ago











2 Answers
2






active

oldest

votes


















3















I want to reference the database in which the stored procedure is stored, even if it is executed [from] another database.




Just use one-part or two-part names in the stored procedure, and it will reference objects in the database containing the stored procedure. In particular,



For static SQL in a stored procedure:



  • Unqualified object names will resolve relative to the schema containing the stored procedure.


  • Two-part names will resolve relative to the database containing the stored procedure.


For dynamic SQL in a stored procedure:



  • Unqualified object names will resolve relative to the default schema of the user identity running the stored procedure (by default, the caller).


  • Two-part names will resolve relative to the database containing the stored procedure.


The db_name() function will return the name of the database containing the stored procedure in both cases.






share|improve this answer
































    2














    Here is a quick example that I put together showing common functions used to get close to what you are looking for.



    /** Create a procedure in master to demonstrate
    DB_NAME()
    OBJECT_SCHEMA_NAME()
    OBJECT_NAME()
    @@PROCID
    **/
    USE [master]
    GO

    CREATE OR ALTER PROCEDURE dbo.uspTestMe
    AS
    BEGIN

    PRINT 'Database: ' + DB_NAME()
    PRINT 'Schema Name: ' + OBJECT_SCHEMA_NAME(@@PROCID)
    PRINT 'Procedure Name: ' + OBJECT_NAME(@@PROCID)

    END

    GO

    /** CHANGE Context to TempDB
    Execute procedure in master
    **/
    USE [tempdb]
    GO

    EXEC master.dbo.uspTestMe

    GO

    /** Cleanup in master **/
    USE [master]
    GO

    DROP PROCEDURE IF EXISTS dbo.uspTestMe





    share|improve this answer























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "182"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Jim Clark is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f236675%2fcan-a-stored-procedure-reference-the-database-in-which-it-is-stored%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3















      I want to reference the database in which the stored procedure is stored, even if it is executed [from] another database.




      Just use one-part or two-part names in the stored procedure, and it will reference objects in the database containing the stored procedure. In particular,



      For static SQL in a stored procedure:



      • Unqualified object names will resolve relative to the schema containing the stored procedure.


      • Two-part names will resolve relative to the database containing the stored procedure.


      For dynamic SQL in a stored procedure:



      • Unqualified object names will resolve relative to the default schema of the user identity running the stored procedure (by default, the caller).


      • Two-part names will resolve relative to the database containing the stored procedure.


      The db_name() function will return the name of the database containing the stored procedure in both cases.






      share|improve this answer





























        3















        I want to reference the database in which the stored procedure is stored, even if it is executed [from] another database.




        Just use one-part or two-part names in the stored procedure, and it will reference objects in the database containing the stored procedure. In particular,



        For static SQL in a stored procedure:



        • Unqualified object names will resolve relative to the schema containing the stored procedure.


        • Two-part names will resolve relative to the database containing the stored procedure.


        For dynamic SQL in a stored procedure:



        • Unqualified object names will resolve relative to the default schema of the user identity running the stored procedure (by default, the caller).


        • Two-part names will resolve relative to the database containing the stored procedure.


        The db_name() function will return the name of the database containing the stored procedure in both cases.






        share|improve this answer



























          3












          3








          3








          I want to reference the database in which the stored procedure is stored, even if it is executed [from] another database.




          Just use one-part or two-part names in the stored procedure, and it will reference objects in the database containing the stored procedure. In particular,



          For static SQL in a stored procedure:



          • Unqualified object names will resolve relative to the schema containing the stored procedure.


          • Two-part names will resolve relative to the database containing the stored procedure.


          For dynamic SQL in a stored procedure:



          • Unqualified object names will resolve relative to the default schema of the user identity running the stored procedure (by default, the caller).


          • Two-part names will resolve relative to the database containing the stored procedure.


          The db_name() function will return the name of the database containing the stored procedure in both cases.






          share|improve this answer
















          I want to reference the database in which the stored procedure is stored, even if it is executed [from] another database.




          Just use one-part or two-part names in the stored procedure, and it will reference objects in the database containing the stored procedure. In particular,



          For static SQL in a stored procedure:



          • Unqualified object names will resolve relative to the schema containing the stored procedure.


          • Two-part names will resolve relative to the database containing the stored procedure.


          For dynamic SQL in a stored procedure:



          • Unqualified object names will resolve relative to the default schema of the user identity running the stored procedure (by default, the caller).


          • Two-part names will resolve relative to the database containing the stored procedure.


          The db_name() function will return the name of the database containing the stored procedure in both cases.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 2 hours ago









          David Browne - MicrosoftDavid Browne - Microsoft

          12.7k733




          12.7k733























              2














              Here is a quick example that I put together showing common functions used to get close to what you are looking for.



              /** Create a procedure in master to demonstrate
              DB_NAME()
              OBJECT_SCHEMA_NAME()
              OBJECT_NAME()
              @@PROCID
              **/
              USE [master]
              GO

              CREATE OR ALTER PROCEDURE dbo.uspTestMe
              AS
              BEGIN

              PRINT 'Database: ' + DB_NAME()
              PRINT 'Schema Name: ' + OBJECT_SCHEMA_NAME(@@PROCID)
              PRINT 'Procedure Name: ' + OBJECT_NAME(@@PROCID)

              END

              GO

              /** CHANGE Context to TempDB
              Execute procedure in master
              **/
              USE [tempdb]
              GO

              EXEC master.dbo.uspTestMe

              GO

              /** Cleanup in master **/
              USE [master]
              GO

              DROP PROCEDURE IF EXISTS dbo.uspTestMe





              share|improve this answer



























                2














                Here is a quick example that I put together showing common functions used to get close to what you are looking for.



                /** Create a procedure in master to demonstrate
                DB_NAME()
                OBJECT_SCHEMA_NAME()
                OBJECT_NAME()
                @@PROCID
                **/
                USE [master]
                GO

                CREATE OR ALTER PROCEDURE dbo.uspTestMe
                AS
                BEGIN

                PRINT 'Database: ' + DB_NAME()
                PRINT 'Schema Name: ' + OBJECT_SCHEMA_NAME(@@PROCID)
                PRINT 'Procedure Name: ' + OBJECT_NAME(@@PROCID)

                END

                GO

                /** CHANGE Context to TempDB
                Execute procedure in master
                **/
                USE [tempdb]
                GO

                EXEC master.dbo.uspTestMe

                GO

                /** Cleanup in master **/
                USE [master]
                GO

                DROP PROCEDURE IF EXISTS dbo.uspTestMe





                share|improve this answer

























                  2












                  2








                  2







                  Here is a quick example that I put together showing common functions used to get close to what you are looking for.



                  /** Create a procedure in master to demonstrate
                  DB_NAME()
                  OBJECT_SCHEMA_NAME()
                  OBJECT_NAME()
                  @@PROCID
                  **/
                  USE [master]
                  GO

                  CREATE OR ALTER PROCEDURE dbo.uspTestMe
                  AS
                  BEGIN

                  PRINT 'Database: ' + DB_NAME()
                  PRINT 'Schema Name: ' + OBJECT_SCHEMA_NAME(@@PROCID)
                  PRINT 'Procedure Name: ' + OBJECT_NAME(@@PROCID)

                  END

                  GO

                  /** CHANGE Context to TempDB
                  Execute procedure in master
                  **/
                  USE [tempdb]
                  GO

                  EXEC master.dbo.uspTestMe

                  GO

                  /** Cleanup in master **/
                  USE [master]
                  GO

                  DROP PROCEDURE IF EXISTS dbo.uspTestMe





                  share|improve this answer













                  Here is a quick example that I put together showing common functions used to get close to what you are looking for.



                  /** Create a procedure in master to demonstrate
                  DB_NAME()
                  OBJECT_SCHEMA_NAME()
                  OBJECT_NAME()
                  @@PROCID
                  **/
                  USE [master]
                  GO

                  CREATE OR ALTER PROCEDURE dbo.uspTestMe
                  AS
                  BEGIN

                  PRINT 'Database: ' + DB_NAME()
                  PRINT 'Schema Name: ' + OBJECT_SCHEMA_NAME(@@PROCID)
                  PRINT 'Procedure Name: ' + OBJECT_NAME(@@PROCID)

                  END

                  GO

                  /** CHANGE Context to TempDB
                  Execute procedure in master
                  **/
                  USE [tempdb]
                  GO

                  EXEC master.dbo.uspTestMe

                  GO

                  /** Cleanup in master **/
                  USE [master]
                  GO

                  DROP PROCEDURE IF EXISTS dbo.uspTestMe






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  Jonathan FiteJonathan Fite

                  4,133818




                  4,133818




















                      Jim Clark is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      Jim Clark is a new contributor. Be nice, and check out our Code of Conduct.












                      Jim Clark is a new contributor. Be nice, and check out our Code of Conduct.











                      Jim Clark is a new contributor. Be nice, and check out our Code of Conduct.














                      Thanks for contributing an answer to Database Administrators Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f236675%2fcan-a-stored-procedure-reference-the-database-in-which-it-is-stored%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

                      Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

                      19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу