Connecting Tech Pros Worldwide Help | Site Map

T-SQL

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 2nd, 2008, 02:45 PM
ventalinks@nospammers.com
Guest
 
Posts: n/a
Default 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 :-)

  #2  
Old October 2nd, 2008, 02:55 PM
Roy Harvey (SQL Server MVP)
Guest
 
Posts: n/a
Default Re: T-SQL

On Thu, 02 Oct 2008 10:44:09 -0400, ventalinks@nospammers.com wrote:
Quote:
>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
  #3  
Old October 2nd, 2008, 03:05 PM
ventalinks@nospammers.com
Guest
 
Posts: n/a
Default Re: T-SQL

Roy Harvey (SQL Server MVP) wrote:
Quote:
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?


Quote:
Roy Harvey
Beacon Falls, CT
  #4  
Old October 2nd, 2008, 03:15 PM
Plamen Ratchev
Guest
 
Posts: n/a
Default 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.TableA AS A
JOIN Database2.dbo.TableB AS B
ON A.keycolumn = B.keycolumn;

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

--
Plamen Ratchev
http://www.SQLStudio.com
  #5  
Old October 2nd, 2008, 03:45 PM
ventalinks@nospammers.com
Guest
 
Posts: n/a
Default Re: T-SQL

Plamen Ratchev wrote:
Quote:
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.TableA AS A
JOIN Database2.dbo.TableB 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.


  #6  
Old October 2nd, 2008, 03:55 PM
Plamen Ratchev
Guest
 
Posts: n/a
Default 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.TableA AS A
JOIN LinkedServer.Database2.dbo.TableB AS B
ON A.keycolumn = B.keycolumn;

More details on linked servers and
sp_addlinkedserver/sp_addlinkedsrvlogin to create linked server and
login mapping:
http://msdn.microsoft.com/en-us/library/ms188279.aspx
http://msdn.microsoft.com/en-us/library/ms190479.aspx
http://msdn.microsoft.com/en-us/library/ms189811.aspx

Also, you can use context switching to gain access to the confidential
table via a different user:
http://msdn.microsoft.com/en-us/libr...6(SQL.90).aspx
http://articles.techrepublic.com.com...1-6158511.html


--
Plamen Ratchev
http://www.SQLStudio.com
  #7  
Old October 2nd, 2008, 04:15 PM
ventalinks@nospammers.com
Guest
 
Posts: n/a
Default Re: T-SQL

Plamen Ratchev wrote:
Quote:
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.
  #8  
Old October 2nd, 2008, 10:15 PM
Erland Sommarskog
Guest
 
Posts: n/a
Default Re: T-SQL

(ventalinks@nospammers.com) writes:
Quote:
Plamen Ratchev wrote:
Quote:
>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.TableA AS A
>JOIN Database2.dbo.TableB 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 malconfiguration. 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@sommarskog.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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.