mike99c
asked on
Syntax error in Access 2000 query
I have inherited a complex SQL query for Access 2000 which I have attached. It is not too important what the query does but I have had to modify it by adding the following statement in line 6 of the first SELECT clause:
(Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId) AS CategoryEnabled
In the final WHERE clause I then use the result of this query by adding the following statement in line 34:
AND CategoryEnabled = True
Unfortunately when I run the query I get the following error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
If I leave the additional WHERE clause statement out the query runs without error but I need it to check the CategoryEnabled flag.
Any help would be appreciated.
(Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId) AS CategoryEnabled
In the final WHERE clause I then use the result of this query by adding the following statement in line 34:
AND CategoryEnabled = True
Unfortunately when I run the query I get the following error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
If I leave the additional WHERE clause statement out the query runs without error but I need it to check the CategoryEnabled flag.
Any help would be appreciated.
SELECT
ItemTexts.Name,
t2.rank,
t1.stockid,
items.*,
(Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId) AS CategoryEnabled
FROM
( items inner join
( SELECT
t1.rank, t1.StockID, pi1.ItemRefID
FROM
priceditems pi1
INNER JOIN
( SELECT
o1.stockid,count(o1.stockid) as rank
FROM
orders AS o1
WHERE
o1.transactionid IN
( SELECT DISTINCT
o2.TransactionID
FROM
PricedItems pi2
LEFT JOIN orders o2 ON pi2.StockID = o2.StockID
WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'') )
GROUP BY o1.stockid ) as t1
ON pi1.stockid=t1.stockid
WHERE
pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974 ) as t2
ON items.refID=t2.ItemRefID )
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
WHERE
ItemTexts.lang='en'
AND CategoryEnabled = True
AND Items.Enable=True
OREDR BY
t2.rank desc
When you run the query without the WHERE clause, do you see CategoryEnabled as one of the output columns?
Is it possible for you to post a sampel of output you get without the WHERE clause?
ASKER
To answer both responses, I don't think it is a typo but for some reason it does not recognise the alias name of CategoryEnabled which was introduced at the end of line 6.
Removing the WHERE clause would certainly not produce CategoryEnabled as an output as the alias would be removed.
Removing the WHERE clause would certainly not produce CategoryEnabled as an output as the alias would be removed.
Why not put the addition WHERE clause in line 6? BTW, with all the SELECTS flying around, it might be helpful to qualify the field CategoryEnabled with its table name which I presume is ItemCategories.
ASKER
Hi GRayL,
Can you please show me what you actually mean. I am not sure how to qualify the CategoryEnabled field with the ItemCategories table. When I tried I kept getting errors because of all the joins. Can you please help me here by showing how I can modify the SQL?
Thanks
Can you please show me what you actually mean. I am not sure how to qualify the CategoryEnabled field with the ItemCategories table. When I tried I kept getting errors because of all the joins. Can you please help me here by showing how I can modify the SQL?
Thanks
Line 6 becomes:
(Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId AND ItemCatgegories.Enable = True) AS CategoryEnabled
Be aware you cannot use an alias which you used in the SELECT portion of a query after THE FROM clause - you have to repeat the whole subquery. CategoryEnabled is an alias. Is is not just as easy to restrict the selection in the subquery??
(Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId AND ItemCatgegories.Enable = True) AS CategoryEnabled
Be aware you cannot use an alias which you used in the SELECT portion of a query after THE FROM clause - you have to repeat the whole subquery. CategoryEnabled is an alias. Is is not just as easy to restrict the selection in the subquery??
ASKER
Thanks for the suggestion GRayL but the solution you have given is not going to work as I need to read the CategoryEnabled alias so that I can restrict the rows returned from the query where CategoryEnabled=False.
I have taken a step back and now realise that I may have over complicated this. I have attached a modifed code which I believe is closer to what I require. The chages are as follows:
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'itemtexts.RefId=items.Ref ID inner join ItemCategories ON ItemCategories.RefID = items.RefID'.
I believe there is a syntax issue with the join that I added. I am more familiar with SQL server so I may have got the syntax a little wrong for Access SQL.
I have taken a step back and now realise that I may have over complicated this. I have attached a modifed code which I believe is closer to what I require. The chages are as follows:
- Replaced nested SELECT query with
ItemCategories.Enable - Added extra inner join statement before last WHERE clause.
INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID - Replaced AND CategoryEnabled = True in last WHERE clause with
AND ItemCategories.Enable = True
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'itemtexts.RefId=items.Ref
I believe there is a syntax issue with the join that I added. I am more familiar with SQL server so I may have got the syntax a little wrong for Access SQL.
SELECT
ItemTexts.Name,
t2.rank,
t1.stockid,
items.*,
ItemCategories.Enable
FROM
( items inner join
( SELECT
t1.rank, t1.StockID, pi1.ItemRefID
FROM
priceditems pi1
INNER JOIN
( SELECT
o1.stockid,count(o1.stockid) as rank
FROM
orders AS o1
WHERE
o1.transactionid IN
( SELECT DISTINCT
o2.TransactionID
FROM
PricedItems pi2
LEFT JOIN orders o2 ON pi2.StockID = o2.StockID
WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'') )
GROUP BY o1.stockid ) as t1
ON pi1.stockid=t1.stockid
WHERE
pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974 ) as t2
ON items.refID=t2.ItemRefID )
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID
WHERE
ItemTexts.lang='en'
AND ItemCategories.Enable = True
AND Items.Enable=True
OREDR BY
t2.rank desc
You have an extra closing parenthesis before
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefI D
Try this
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefI
Try this
SELECT
ItemTexts.Name,
t2.rank,
t1.stockid,
items.*,
ItemCategories.Enable
FROM items
inner join
( SELECT
t1.rank, t1.StockID, pi1.ItemRefID
FROM
priceditems pi1
INNER JOIN
( SELECT o1.stockid,count(o1.stockid) as rank
FROM
orders AS o1
WHERE
o1.transactionid IN
( SELECT DISTINCT o2.TransactionID
FROM PricedItems pi2
LEFT JOIN orders o2 ON pi2.StockID = o2.StockID
WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'')
)
GROUP BY o1.stockid )
as t1
ON pi1.stockid=t1.stockid
WHERE pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974
) as t2
ON items.refID=t2.ItemRefID
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID
WHERE
ItemTexts.lang='en'
AND ItemCategories.Enable = True
AND Items.Enable=True
OREDR BY
t2.rank desc
ASKER
Hi iparul,
Thanks for the new query but when I tried it I got the following error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'items.refID=t2.ItemRefID left join itemTexts on itemtexts.RefId=items.RefI D inner join ItemCategories ON ItemCategories.RefID = items.RefID'.
Remember that the query is for Access 2000. I think it may need extra brackets but I am not sure where.
Thanks for the new query but when I tried it I got the following error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'items.refID=t2.ItemRefID left join itemTexts on itemtexts.RefId=items.RefI
Remember that the query is for Access 2000. I think it may need extra brackets but I am not sure where.
ASKER
Can anyone please assist me with the attached query?
I am not familiar with the Access 200 SQL JOIN syntax. Can someone please assist me with the join syntax?
I am not familiar with the Access 200 SQL JOIN syntax. Can someone please assist me with the join syntax?
SELECT
ItemTexts.Name,
t2.rank,
t1.stockid,
items.*,
ItemCategories.Enable
FROM items
inner join
( SELECT
t1.rank, t1.StockID, pi1.ItemRefID
FROM
priceditems pi1
INNER JOIN
( SELECT o1.stockid,count(o1.stockid) as rank
FROM
orders AS o1
WHERE
o1.transactionid IN
( SELECT DISTINCT o2.TransactionID
FROM PricedItems pi2
LEFT JOIN orders o2 ON pi2.StockID = o2.StockID
WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'')
)
GROUP BY o1.stockid )
as t1
ON pi1.stockid=t1.stockid
WHERE pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974
) as t2
ON items.refID=t2.ItemRefID
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID
WHERE
ItemTexts.lang='en'
AND ItemCategories.Enable = True
AND Items.Enable=True
OREDR BY
t2.rank desc
Hi
Sorry for getting back to you late. Anyways, I don't have Access 2000, so I am unable to test the query. But I tried to format the query with some parenthesis and put a ; at th end. Try and see if this works:
Sorry for getting back to you late. Anyways, I don't have Access 2000, so I am unable to test the query. But I tried to format the query with some parenthesis and put a ; at th end. Try and see if this works:
SELECT
ItemTexts.Name,
t2.rank,
t1.stockid,
items.*,
ItemCategories.Enable
FROM (((items
inner join
( SELECT
t1.rank, t1.StockID, pi1.ItemRefID
FROM
priceditems pi1
INNER JOIN
( SELECT o1.stockid,count(o1.stockid) as rank
FROM
orders AS o1
WHERE
o1.transactionid IN
( SELECT DISTINCT o2.TransactionID
FROM PricedItems pi2
LEFT JOIN orders o2 ON pi2.StockID = o2.StockID
WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'')
)
GROUP BY o1.stockid )
as t1
ON pi1.stockid=t1.stockid
WHERE pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974
) as t2
ON items.refID=t2.ItemRefID)
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID)
INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID)
WHERE
ItemTexts.lang='en'
AND ItemCategories.Enable = True
AND Items.Enable=True
ORDER BY
t2.rank desc ;
ASKER
Hi,
Thanks for getting back to me. We are getting a lot closer. I executed the query and there were no errors. However the query did not return any results which is unexpected as it should return several rows.
Do you think the last inner join is in the right place?
Thanks for getting back to me. We are getting a lot closer. I executed the query and there were no errors. However the query did not return any results which is unexpected as it should return several rows.
Do you think the last inner join is in the right place?
I think the joins are in right place. If the query is not returning any results, then there is a possibility that all the conditions are not being met. Did you check that all the join conditions and where conditions are okay and that there are rows that satisfy those conditions?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Okay
Here's what I would do:
Starting from the query that worked, and looking at the final query we have which doesn't give any errors, try adding one clause (where condition, join, additional col in select etc) one at a time and see if it gives the expected results. Here's the order I would do it
a) Add ItemCategories.Enable in the select statement
b) Add inner join statement
c) Add condition for ItemCategories.Enable = True
See if this helps and let me know.
Here's what I would do:
Starting from the query that worked, and looking at the final query we have which doesn't give any errors, try adding one clause (where condition, join, additional col in select etc) one at a time and see if it gives the expected results. Here's the order I would do it
a) Add ItemCategories.Enable in the select statement
b) Add inner join statement
c) Add condition for ItemCategories.Enable = True
See if this helps and let me know.
If this runs fine without the WHERE clause as you stated, scrutinize the WHERE clause for typos or incorrect field names.