T-SQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ventalinks@nospammers.com

    T-SQL

    Hello,

    I need to write a stored procedure that has two different user
    connections to two different databases. Both db's are on the same one
    server. Is this possible to create using Transact-SQL commands in a
    stored procedure?

    Thanks :-)
  • Roy Harvey (SQL Server MVP)

    #2
    Re: T-SQL

    On Thu, 02 Oct 2008 10:44:09 -0400, ventalinks@nosp ammers.com wrote:
    >I need to write a stored procedure that has two different user
    >connections to two different databases. Both db's are on the same one
    >server. Is this possible to create using Transact-SQL commands in a
    >stored procedure?
    A stored procedure runs on one connection, but it can reference
    multiple databases on the same instance of SQL Server. If you
    describe what you actually need to do someone may be able to suggest
    an approach.

    Roy Harvey
    Beacon Falls, CT

    Comment

    • ventalinks@nospammers.com

      #3
      Re: T-SQL

      Roy Harvey (SQL Server MVP) wrote:
      A stored procedure runs on one connection, but it can reference
      multiple databases on the same instance of SQL Server. If you
      describe what you actually need to do someone may be able to suggest
      an approach.
      Thans for your reply. I wished to creat a report. Some data of the
      report is confidential data. It is in another database requiring
      different username and password. I am given all necessary usernames and
      passwords. I would want to write the report that will combine these
      data and regular data. Is this possibility?


      Roy Harvey
      Beacon Falls, CT

      Comment

      • Plamen Ratchev

        #4
        Re: T-SQL

        Since both databases are on the same server, you can reference both in a
        single query (assuming user has permissions to both, etc.):

        SELECT <columns>
        FROM Database1.dbo.T ableA AS A
        JOIN Database2.dbo.T ableB AS B
        ON A.keycolumn = B.keycolumn;

        Note that each table is prefixed with database and schema name.

        --
        Plamen Ratchev

        Comment

        • ventalinks@nospammers.com

          #5
          Re: T-SQL

          Plamen Ratchev wrote:
          Since both databases are on the same server, you can reference both in a
          single query (assuming user has permissions to both, etc.):
          >
          SELECT <columns>
          FROM Database1.dbo.T ableA AS A
          JOIN Database2.dbo.T ableB AS B
          ON A.keycolumn = B.keycolumn;
          >
          Note that each table is prefixed with database and schema name.

          Hello,

          There are two different usernames and one has not access to the other
          database. I am getting this error. I think this is do to access
          privilige limitation:


          The server principal "x" is not able to access the database "y" under
          the current security context.


          Comment

          • Plamen Ratchev

            #6
            Re: T-SQL

            One way would be to create a linked server to the confidential database
            using the user/pwd provided, then in a query you can reference the
            linked server:

            SELECT <columns>
            FROM Database1.dbo.T ableA AS A
            JOIN LinkedServer.Da tabase2.dbo.Tab leB AS B
            ON A.keycolumn = B.keycolumn;

            More details on linked servers and
            sp_addlinkedser ver/sp_addlinkedsrv login to create linked server and
            login mapping:
            Linked servers enable SQL Server and Azure SQL Managed Instance to read from and execute data in remote data sources.


            Creates or updates a mapping between a login on the local instance of SQL Server and a security account on a remote server.


            Also, you can use context switching to gain access to the confidential
            table via a different user:




            --
            Plamen Ratchev

            Comment

            • ventalinks@nospammers.com

              #7
              Re: T-SQL

              Plamen Ratchev wrote:
              One way would be to create a linked server to the confidential database
              using the user/pwd provided, then in a query you can reference the
              linked server:

              Thanks for all of those links. I will research them and see if they
              help in my case.

              Comment

              • Erland Sommarskog

                #8
                Re: T-SQL

                (ventalinks@nos pammers.com) writes:
                Plamen Ratchev wrote:
                >Since both databases are on the same server, you can reference both in a
                >single query (assuming user has permissions to both, etc.):
                >>
                >SELECT <columns>
                >FROM Database1.dbo.T ableA AS A
                >JOIN Database2.dbo.T ableB AS B
                > ON A.keycolumn = B.keycolumn;
                >>
                >Note that each table is prefixed with database and schema name.
                >
                >
                Hello,
                >
                There are two different usernames and one has not access to the other
                database. I am getting this error. I think this is do to access
                privilige limitation:
                >
                >
                The server principal "x" is not able to access the database "y" under
                the current security context.
                Before you do anything else, you should speak with your DBA. Maybe this
                is just a case of malconfiguratio n. May the login x should be a user in
                y as well. In that case, what Plamen suggested will work.

                If the configuration is correct, you will need to set up a few things.
                Or maybe rather someone with the powers-to-be will have to. There is a
                longer article on my web site that discusses permissions in general,
                including cross-database access: http://www.sommarskog.se/grantperm.html
                that should get you started.


                --
                Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

                Links for SQL Server Books Online:
                SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
                SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
                SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

                Comment

                Working...