June 1, 2011 at 1:45 pm
I am the classic accidental DBA at a medium size company and i have a join question. If this is not the correct spot for this post please move it, Mods.
I am trying to join tables from two different databases. here is a simple example that i cannot get to work and i get the error: The column prefix 'Table1' does not match with a table name or alias name used in the query. I have also tried a left join as well as listing two columns to narrow it down. Please point me in the right direction. I may be going about this the wrong way basically i am needing to link data from different DB's together with a select statement for a conversion and i am a little lost as to why i can join within a DB but not two or more DB's together.
Thanks in advance
Chris
select * from DB1.dbo.table1 inner join DB2.dbo.Table2 on table1.acctnumber = table2.acctnumber
June 1, 2011 at 1:48 pm
unless you alias the table, you must use the whole 3 part naming convention(Database.Schema.Table.ColumnName) toi identify the columns in the joins:
here's the right way, and then again with aliases for the tables:
select *
from DB1.dbo.table1
inner join DB2.dbo.Table2
on DB1.dbo.table1.acctnumber = DB2.dbo.Table2.acctnumber
select *
from DB1.dbo.table1 MyAliasA
inner join DB2.dbo.Table2 MyAliasB
on MyAliasA.acctnumber = MyAliasB.acctnumber
Lowell
June 1, 2011 at 1:49 pm
You're using the table name only in your join predicate: table1.acctnumber = table2.acctnumber
But those table names are interpreted as being local tables.
The easiest would be to assign an alias to the three-part-table name:
select * from DB1.dbo.table1 table1 inner join DB2.dbo.Table2 Table2 on table1.acctnumber = table2.acctnumber
June 1, 2011 at 2:11 pm
thanks guys for the quick response, i tried that before, but when i tried it i was doing my joins out of order. silly mistake.
also as a side note any suggestions on good SQL books for somebody in my situation?
June 1, 2011 at 2:17 pm
For reallllllllly basic you can go to w3schools.
After that you can read and search problems on this forum... and then ask questions for what you don't find.
June 1, 2011 at 3:01 pm
true and i am sure i will be searching the forum in the future as i run into new SQL and SQL server problems.
June 1, 2011 at 4:17 pm
cw.izatt (6/1/2011)
also as a side note any suggestions on good SQL books for somebody in my situation?
Check BOL - Books On Line, it comes with SQL Server for free. If in need of some deeper digging try Microsoft Knowledge Base.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.June 7, 2011 at 8:24 am
BoL has been downloaded thanks for that.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply