SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Query - Join on a table in another database?


SQL Query - Join on a table in another database?

Author
Message
McSQL
McSQL
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1274 Visits: 301
Hey all,


I have been working in a test environment and the following statement works fine (both tables are on the same server).
What I now need to do, to roll this part of the job live is to have a similar query for tables in 2 different databases. (i.e. employeemasterholdingarea is on server1, cutovertransfer is on server2 in live)


Update a
Set Transferflag=1
From
EmployeeMasterHoldingArea a
Inner Join
CutoverTransfer b On a.EmployeeMasterPayrollNumber=b.Employee_Id


A quick overview of what I am trying to do;

When an employee id is found to be present in one table (Cutovertransfer), a bit marker will be updated in the other table (employeemasterholdingarea) to say so.

All tables are identical in live, with the only exception that the live table is on a different database and neither can be moved.


I have tried to do the [ServerName].[DatabaseName].dbo.[TableName].[ColumnName] type query which doesn't work.


I have already set up the other server\database as a linked server (entitled "ospsq1cutover") to run some distributed queries in another part of the job, if this can be used?
Not sure how to perform a join on an openquery, if this is even the right way to go about it.


Any help would be greatly appreciated.


thanks
McSQL
McSQL
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1274 Visits: 301
Sorry, I got it to work with a small change to the method I just said didnt work! Blush


If anyone's interested;

Update a
Set Transferflag=1
From
EmployeeMasterHoldingArea a
Inner Join
[osp-sq1].cutover.dbo.cutovertransfer b On a.EmployeeMasterPayrollNumber=b.employee_ID

*(With osp-sq1 being "server2")


Thanks and sorry if I wasted anyones time Smile
McSQL
McSQL
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1274 Visits: 301
PS, Still not sure if this is the best method to use.
It does work, but if anyone has another method which may be more efficient, then please post a reply.

thanks again
Steve Jones
Steve Jones
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: Administrators
Points: 149160 Visits: 19448
I'd set an view up on the linked server, select a, b, c from newserver.newdb.newtable, and then use the view for updates. That way you won't have to change much code as the table moves to a new server, or even comes back to another database on this server.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search