mossmis
asked on
How to query data from one SQL DB from another SQL DB
I have a single SQL server with several databases on it. There are two databases I am actively using. There is a stored procedure I want to create that gathers certain data from one database and dumps into temporary tables into another. Or to better rephrase the question I want to be a be to run a query from one database that gathers data from another SQL database that is on the same server. How can I go about this? Some of the examples I found so far are not very clear.
ie. This was hard to follow if it is applicable:
https://www.experts-exchange.com/questions/22036718/Connect-another-SQL-Server-Within-select-Query.html
ie. This was hard to follow if it is applicable:
https://www.experts-exchange.com/questions/22036718/Connect-another-SQL-Server-Within-select-Query.html
>> I want to create that gathers certain data from one database and dumps into temporary tables into another. <<
You don't necessarily need to copy into the second db. You can just reference the original data in its original db, even in JOINs, for example:
SELECT t1.col1, t2.col3, ...
FROM dbo.table_in_same_db t1
INNER JOIN other_db.dbo.table_in_othe r_db t2 ON
t2.join_col = t1.join_col
You don't necessarily need to copy into the second db. You can just reference the original data in its original db, even in JOINs, for example:
SELECT t1.col1, t2.col3, ...
FROM dbo.table_in_same_db t1
INNER JOIN other_db.dbo.table_in_othe
t2.join_col = t1.join_col
ASKER
I get this error trying tigin44's solution:
Could not find server 'mySQLServer' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver
Is running that procedure necessary or Did I do something wrong? What kind of permissions do I need to assign to the user to the second database if I will be only reading from it?
Could not find server 'mySQLServer' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver
Is running that procedure necessary or Did I do something wrong? What kind of permissions do I need to assign to the user to the second database if I will be only reading from it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By the way I assume that the user running the query have the enough privilages on both databases...
ASKER
OK, I think I'm almost there. the DATABASENAME.SCHEMA.OBJECT NAME, seems to be working ,however I don't have the right permissions setup for the user. I have been trying to add this user to have access to to second database but I'm getting:
The server principal "SQLUSER" is not able to access the database "NEWSQLDB" under the current security context.
I only want this user, SQLUSER, to have read access. Where should I be adding this?
The server principal "SQLUSER" is not able to access the database "NEWSQLDB" under the current security context.
I only want this user, SQLUSER, to have read access. Where should I be adding this?
ASKER
The "DATABASENAME.SCHEMA.OBJEC TNAME" utimitley worked. I was having authentications issue as I was trying to use mixed mode Windows/SQL authentication and my SQL user could not connect to the second database. I ended up using only windows authentication and I was able to connect to both. I guess using windows authentication is more secure; just I little more work giving specific users access.
SERVERNAME.DATABASENAME.SC
so from database A you may refenece to a table from database B as
SELECT *
FROM B.dbo.tableName