Join

  • Hi,

    I have two tables employee(employeeId,LocationID) and employeelocation(employeeId,Locationcode).

    Both these tables exists in doff database. LocationCode in EmployeeLocation table has char values like XA,Ca etc and in employee table it has ID rather than Code. So how can I put join on these two columns of these two tables. this Location ID and LocationCode is stored in a table Location(LocationID, LocationCode)..

    plzz give a solution

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Plzz follow this: http://www.sqlservercentral.com/articles/Best+Practices/61537/

    😉

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • You have to include the database name if the database resides on the same server:

    SELECT *

    FROM MyDatabase.MySchema.MyTable;

    That will get it done. You can then join on the appropriate column.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Using the information given you can do something like this

    SELECT *

    FROM OneDatabase.dbo.EmployeeLocation el

    JOIN SomeDatabase.dbo.Location l ON el.locationcode = l.locationcode

    JOIN AnotherDatabase.dbo.Employee e ON el.employeeID = e.employeeID

    AND l.locationID = e.locationID

    You need to change the names of the databases and include the fields that you need insteadof the asterisk.

    I'm assuming that your databases are on the same server.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply