Using sproc from one database to access tables from different database

  • Hi:

    How do you use the sproc located in one database to access tables located in a different database?

    The statement (located in database1)

    USE [database_name1]

    worked while the sproc was located in the same as tables,

    then the statement (located in database1) was changed to

    USE [database_name2]

    this did not work, it changed back to original database_name1.

    What changes, do you need to make in sproc (in database1), to access tables (in database2)?

    Thanks

    Balv

  • You need to reference the objects themselves in the other database. For example:

    SELECT ...

    FROM dbo.Table1

    Accesses Table1 in the current database.

    SELECT ...

    FROM db1.dbo.Table1

    Accesses Table1 in db1 regardless of the current database context for the procedure.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Smashing...Perfect.

    Thanks:cool:

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply