How to do an INNER JOIN in SSIS Excel Source component - sql

SELECT [Sheet1$].ID,
CLng([Sheet1$].RecordID) AS RecordID,
[Sheet1$].col1,
[Sheet1$].col2,
[Sheet1$].col3,
[Sheet1$].col4,
[Sheet1$].col5,
[Sheet2$].Name
FROM [Sheet1$]
INNER JOIN [Sheet2$] ON
[Sheet1$].RecordID = [Sheet2$].RecordID
I have the above sample SQL Command in an SSIS Excel Source component. As seen in that query i'm doing an inner join on two excel sheets (Sheet1 and Sheet2) in the same workbook.
At this point the query executes well with out any errors.
However, i am not able to join on a 3rd sheet (Sheet3). When i try to inner join on sheet3, i get the following error message.
An OLE DB record is available. Source: "Microsoft Access Database Engine" Hresult: 0x80040E14 Description: "Syntax error (missing operator) in query expression '[Sheet1$].RecordID = [Sheet2$].ReportID
INNER JOIN [Sheet3$] ON
[Sheet1$].RecordID = [Sheet3$].RecordID'
So i am basically unable to do an inner join on two or more excel sheets. I'm only able to inner join on one excel sheet. The syntax i am using works in SQL Server, so i am wondering
if its supposed to work in a SSIS Excel source SQL Command as well since it seems to be using the Microsoft Access Database Engine.
Below is the query with the second join that is generating the above error:
SELECT [Sheet1$].ID,
CLng([Shee1$].RecordID) AS RecordID,
[Sheet1$].col1,
[Sheet1$].col2,
[Sheet1$].col3,
[Sheet1$].col4,
[Sheet1$].col5,
[Sheet2$].Name
FROM [Sheet1$]
INNER JOIN [Sheet2$] ON
[Sheet1$].RecordID = [Sheet2$].RecordID
INNER JOIN [Sheet3$] ON
[Sheet1$].RecordID = [Sheet3$].RecordID

Ok, i was doing it the wrong way. Microsoft access database engine used by the SSIS Excel Source component handles joins differently than SQL Server.
Apparently, you need to have n - 2 left parentheses after the from
clause and one right parenthesis before the start of each new join
clause except for the first, where n is the number of tables being
joined together.
The reason is that Access's join syntax supports joining only two
tables at a time, so if you need to join more than two you need to
enclose the extra ones in parentheses.
Quoted from Access-SQL: Inner Join with multiple tables
And confirmed at http://office.microsoft.com/en-001/access-help/inner-join-operation-HA001231487.aspx
So the below query now works
SELECT [Sheet1$].ID,
CLng([Shee1$].RecordID) AS RecordID,
[Sheet1$].col1,
[Sheet1$].col2,
[Sheet1$].col3,
[Sheet1$].col4,
[Sheet1$].col5,
[Sheet2$].Name
FROM (([Sheet1$])
INNER JOIN [Sheet2$] ON [Sheet1$].RecordID = [Sheet2$].RecordID)
INNER JOIN [Sheet3$] ON [Sheet1$].RecordID = [Sheet3$].RecordID

Lets try to cheat:
SELECT
CLng(x.RecordID) AS RecordID,
x.col1,
x.col2,
x.col3,
x.col4,
x.col5,
x.Name
FROM (
SELECT
[Sheet1$].RecordID,
[Sheet1$].col1,
[Sheet1$].col2,
[Sheet1$].col3,
[Sheet1$].col4,
[Sheet1$].col5,
[Sheet2$].Name
FROM [Sheet1$]
INNER JOIN [Sheet2$] ON
[Sheet1$].RecordID = [Sheet2$].RecordID
) as x
INNER JOIN [Sheet3$] ON
x.RecordID = [Sheet3$].RecordID

Related

Syntax error on join operation access SQL

I'm trying to do some join operations in sql access but I keep on getting the join operation error. At first it was just the JOIN alone, but then I realized I had to add the INNER which I did but it didn't resolve the error.
Code below:
SELECT Formula.*, Ingred.[Europe Ban]
FROM [Ingred]
INNER JOIN Ingred ON Formula.[Ingredient] = Ingred.Ingredients;
Presumably, you want Formula in the FROM clause, not Ingred twice:
SELECT Formula.*, Ingred.[Europe Ban]
FROM Formula INNER JOIN
Ingred
ON Formula.[Ingredient] = Ingred.Ingredients;

Access crashes then query design button clicked on written sql query

The query works pretty fine, I can view result as a table and sql code on his own without any problem. However then I try to press query design button, it crashed without saying why.
My code is pretty simple, after I added one more left join out of A subquery such error started to appear. This way last left join definitely cause the problem. Tried to join without using subquery but I get problem saying about ambiguous outer join. I'm newbie with access but I heard about several bugs in that program, any suggestion how to fix?
This problem query:
select A.*,targetresp.*
from (
SELECT *
FROM target INNER JOIN ((source INNER JOIN InstanceList
ON source.INFO_SYSTEM_TYPE_CD = InstanceList.INFO_SYSTEM_TYPE_CD)
INNER JOIN (N_table_transform INNER JOIN S2T
ON N_table_transform.N_table = S2T.N_table)
ON source.ID = S2T.source_id)
ON target.id = S2T.target_id ) as A
left join targetresp
on a.target_TableName = targetresp.tablename;
SELECT InstanceList.*
FROM
N_table_transform
INNER JOIN (((S2T INNER JOIN target
ON S2T.target_id = target.Id)
LEFT JOIN targetresp ON target.target_TableName = targetresp.target_TableName)
INNER JOIN (InstanceList
INNER JOIN source ON InstanceList.INFO_SYSTEM_TYPE_CD = source.INFO_SYSTEM_TYPE_CD)
ON S2T.source_id = source.Id)
ON N_table_transform.N_table = S2T.N_table;
I just use the access 2013 designer and build the query.
On the bottom switch between SQL and DESIGNER

Access SQL syntax error (missing operator) when query contains multiple JOINs

I'm trying to make an UPDATE query (using Access 2013) that calculates a value based on values stored in two separate linked tables. Here is the code I'm using:
UPDATE tblCreatures
INNER JOIN tblRole ON tblCreatures.Role = tblRole.RoleName
INNER JOIN tblRank ON tblCreatures.Rank = tblRank.RankName
SET tblCreatures.HP = ((tblRole.Level_0_HP + (tblCreatures.NominalLevel * tblRole.BonusHP)) * tblRank.HP_Multiplier);
This gives me a syntax error, saying
Syntax error (missing operator) in query expression "tblCreatures.Role = tblRole.RoleName INNER JOIN tblRank ON tblCreatures.Rank = tblRank.RankNam"
(and yes, it cuts off at RankNam, not RankName)
Testing things out, if I remove one of the inner joins (and thus all references to that table) then the update query works just fine, but if I put the other inner join back in, I continuously get this same syntax error. I don't understand why... I should be able to put two inner joins next to each other, shouldn't I?
Access SQL requires parentheses when a query contains multiple JOINs. If you build the query in Access' query designer it will look like this (reformatted for clarity):
UPDATE
(
tblCreatures
INNER JOIN
tblRole
ON tblCreatures.Role = tblRole.RoleName
)
INNER JOIN
tblRank
ON tblCreatures.Rank = tblRank.RankName
SET tblCreatures.HP = ((tblRole.Level_0_HP + (tblCreatures.NominalLevel * tblRole.BonusHP)) * tblRank.HP_Multiplier);

syntax error in from clause in nested joins in an ms access

I am getting a Syntax Error in FROM Clause dialog box when I try to run the following query in MS-Access 2010:
SELECT
Contact_addresses.AddressID,
Contact_addresses.ContactID,
Contact_addresses.Address1,
Contact_addresses.Address2,
Contact_addresses.City,
Contact_addresses.State,
Contact_addresses.Zip,
Owner_Operator.FIRM_NAME,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Contact_addresses
(
LEFT JOIN
(SELECT
Owner_Operator.CONTACT_ID,
Owner_Operator.FIRM_NAME
FROM Owner_Operator)
ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID
)
LEFT JOIN
(SELECT
Official_Correspondent.CONTACT_ID,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Official_Correspondent)
ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
;
When I dismiss the dialog box, access highlights the ( after FROM Contact_addresses.
I know that I need parentheses with multiple nested joins in Access, but can someone please explain the concepts of how this works, in addition to showing how to fix whatever the problem is?
The relevant aspects of the schema of the underlying tables should be clear from the SQL SELECT statements.
I think this should work, but I lack access to test. General syntax to hold to
From table
left join (statement) alias
on table.col = alias.col
left join ...
Altering your statement to this:
FROM Contact_addresses
LEFT JOIN
(SELECT
Owner_Operator.CONTACT_ID,
Owner_Operator.FIRM_NAME
FROM Owner_Operator) Owner_Operator
ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID
LEFT JOIN
(SELECT
Official_Correspondent.CONTACT_ID,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Official_Correspondent) Official_Correspondent
ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
;
I've added the table alias to match what you call it in your join and I removed the offending set of brackets.
The problem was in the location of the opening brackets ((. They need to be immediately after the first FROM. Here is what works:
SELECT Contact_addresses.AddressID, Contact_addresses.ContactID, Contact_addresses.Address1, Contact_addresses.Address2, Contact_addresses.City, Contact_addresses.State, Contact_addresses.Zip, Owner_Operator.FIRM_NAME, Official_Correspondent.FIRST_NAME, Official_Correspondent.LAST_NAME, Official_Correspondent.SUBACCOUNT_COMPANY_NAME, Official_Correspondent.PHONE_NUMBER
FROM ((Contact_addresses
LEFT JOIN
(SELECT
Owner_Operator.CONTACT_ID,
Owner_Operator.FIRM_NAME
FROM Owner_Operator) AS Owner_Operator
ON Contact_addresses.ContactID=Owner_Operator.CONTACT_ID
)
LEFT JOIN
(SELECT
Official_Correspondent.CONTACT_ID,
Official_Correspondent.FIRST_NAME,
Official_Correspondent.LAST_NAME,
Official_Correspondent.SUBACCOUNT_COMPANY_NAME,
Official_Correspondent.PHONE_NUMBER
FROM Official_Correspondent) AS Official_Correspondent
ON Contact_addresses.ContactID=Official_Correspondent.CONTACT_ID
)
;

MS Access SQL Query - syntax error(missing operator) in query expression

I am trying to understand why my query(below) displays an error message in MS Access Sql query editor(sqlview) when I run it.
SELECT *
FROM tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE]
INNER JOIN tblAttribute
ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID
As far as I know the script below if I delete either of the INNER join lines. For instance, this script runs with no errors
SELECT *
FROM tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE]
And so does this
SELECT *
FROM tblUSPS
INNER JOIN tblAttribute ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID
But when I combine, something goes wrong and I am unable to find it so I would like some help identifying this please.
Access has strong opinions on parentheses.
SELECT *
FROM
(tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE] )
INNER JOIN tblAttribute
ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID