473,385 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 :-)
Oct 2 '08 #1
7 2670
On Thu, 02 Oct 2008 10:44:09 -0400, ve********@nospammers.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
Oct 2 '08 #2
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
Oct 2 '08 #3
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
Oct 2 '08 #4
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.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.
Oct 2 '08 #5
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
Oct 2 '08 #6
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.
Oct 2 '08 #7
(ve********@nospammers.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.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, es****@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

Oct 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi; I'm brand spanking new to sqlserver ( nice so far ). I need to make a simple data change across a list of tables. Basically replace an old date with a new date. However, the people I am...
2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
1
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
7
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE...
8
by: David Lozzi | last post by:
I'm fairly new to ASP.Net 2.0 SQLDatasource objects. It defaults using TSQL statments for the SELECT, INSERT, UPDATE, DELETE commands, which is great and it works. However, I've always been taught...
0
by: DR | last post by:
Unable to start TSQL Debugging. Could not attach to SQL Server Process on 'srvname'. The RPC server is unavailable. I get this error when I try to run a SQL Server Project with a CLR stored...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.