Unknown SQL Syntax Error near Keyword 'left' - sql

I am getting a syntax error "incorrect syntax near the keyword 'left'," but I don't know what I am doing wrong. I am trying to run an update query to set French addresses to 5. What am I missing?
UPDATE
Persons p
left join States s on p.StateID = p.pkState
SET
p.International = 5
WHERE
s.CountryRegionCodeID = 'FR';

The correct syntax in SQL Server uses a FROM clause:
UPDATE p
SET p.International = 5
FROM Persons p JOIN
States s
ON p.StateID = p.pkState
WHERE s.CountryRegionCodeID = 'FR';
Note: I changed the LEFT JOIN to a JOIN. The WHERE clause is turning the outer join into an inner join anyway.

Related

MS Access - Syntax error (missing operator) in query expression

SELECT * FROM exclusivity
left join patent on (exclusivity.[Appl_Type]=patent.[Appl_Type] AND exclusivity.[Appl_No]=patent.[Appl_No] AND exclusivity.[Product_No]=patent.[Product_No])
left join products on (exclusivity.[Appl_Type]=products.[Appl_Type] AND exclusivity.[Appl_No]=products.[Appl_No] AND exclusivity.[Product_No]=products.[Product_No]);
The above query gives Syntax error
(missing operator) in query expression 'exclusivity.[Appl_Type]=patent.[Appl_Type] AND exclusivity.[Appl_No]=patent.[Appl_No] AND exclusivity.[Product_No]=patent.[Product_No])
left join products on (exclusivity.[Appl_Type]=products.[Appl_Type] AND exclusivity.[Appl_No]=products.[Appl_No] AND exclusivity.[Product_No]=products.[Product_No]);'
What could be possible reason?
MS Access has weird requirements for parentheses around joins:
SELECT *
FROM (exclusivity left join
patent
on exclusivity.[Appl_Type] = patent.[Appl_Type] AND
exclusivity.[Appl_No] = patent.[Appl_No] AND
exclusivity.[Product_No] = patent.[Product_No]
) left join
products
on exclusivity.[Appl_Type] = products.[Appl_Type] AND
exclusivity.[Appl_No] = products.[Appl_No] AND
exclusivity.[Product_No] = products.[Product_No];

Updating upon a join

I can simply use a select statment on a join to pull results I'd Like using:
Select * from RESULTS (NOLOCK) left join orders on Results.ordno = orders.ordno
left join folders on folders.folderno = orders.folderno
left join pranaparms on folders.prodcode = pranaparms.prodcode and results.analyte = pranaparms.analyte
WHERE Results.s <> 'OOS-A' and Results.Final Between pranaparms.LOWERQCLIMIT and pranaparms.UPPERQCLIMIT and (pranaparms.LOWERQCLIMIT IS NOT NULL and pranaparms.UPPERQCLIMIT IS NOT NULL)
and results.ordno in (1277494)
However, is there a convenient way which I could do an update on the selected fields?
I have tried this so far:
Update RESULTS (NOLOCK) left join orders on Results.ordno = orders.ordno
left join folders on folders.folderno = orders.folderno
left join pranaparms on folders.prodcode = pranaparms.prodcode and results.analyte = pranaparms.analyte
set Results.S = 'OOS-B'
WHERE Results.s <> 'OOS-A' and Results.Final Between pranaparms.LOWERQCLIMIT and pranaparms.UPPERQCLIMIT and (pranaparms.LOWERQCLIMIT IS NOT NULL and pranaparms.UPPERQCLIMIT IS NOT NULL)
and results.ordno in (1277494)
However, it is passing an error indicating "Incorrect syntax near '('"
Is there a way for me to update off of this join or will I need to do tables individually?
Your select syntax suggests SQL Server. The correct update syntax in SQL Server is:
update r
set S = 'OOS-B'
from results r left join
orders o
on r.ordno = o.ordno left join
folders f
on f.folderno = o.folderno left join
pranaparms p
on f.prodcode = p.prodcode and r.analyte = p.analyte
where r.s <> 'OOS-A' and
r.Final Between p.LOWERQCLIMIT and p.UPPERQCLIMIT and
(p.LOWERQCLIMIT IS NOT NULL and p.UPPERQCLIMIT IS NOT NULL) and
r.ordno in (1277494);
Notes:
Table aliases make the query much easier to write and to read.
Do not use NOLOCK unless you know what you are doing. Given that you don't know the syntax rules for update for the database you are using, I will guess that you don't understand locks either.
Your WHERE conditions are turning the outer joins into inner joins. You should just specify the correct join type -- and probably move the conditions to the on clauses. I've left the logic as you wrote it.

Join multiple tables in SQL - [Troubleshoot]

So I want to join three (or more) tables to make my life easier in Access.
however, when I add the code:
SELECT * FROM tbl_Inventory i
LEFT JOIN tbl_FlameConditions1 fc1
ON i.ID = fc1.SampleID
LEFT JOIN tbl_SolventComponents1 sc1
ON i.ID = sc1.SampleID;
to my query in Access, it gives me an error message:
"Syntax error (missing operator) in query expression 'i.ID = fc1.SampleID
LEFT JOIN tbl_SolventComponents1 sc1
ON i.ID = sc1.SampleI' ".
And I forgot about the D and ; in the statement intentionally, since this is what Access gives me...
Does anyone know how to fix this? I already tried various different combinations. Also, if I only try to join 2 tables (either one), it works fine with the code I got.
You would need to add parenthesis in Access:
SELECT * FROM (tbl_Inventory i
LEFT JOIN tbl_FlameConditions1 fc1
ON i.ID = fc1.SampleID)
LEFT JOIN tbl_SolventComponents1 sc1
ON i.ID = sc1.SampleID;

How to UPDATE a table with NESTED JOIN. I am getting syntax error in MS Access

I am trying to UPDATE a table in MS Access but the table I cannot figure out what my Syntax Error is! MS Access throws "Syntax Error in Join expression" and highlights my SET command. Can anyone help spot my mistake?
UPDATE tbl_Items
INNER JOIN
(SELECT *
FROM tbl_Inventory
INNER JOIN tbl_Inputs
ON tbl_Inventory.class = tbl_Inputs.class)
ON tbl_Items.PRODUCT_ID = tbl_Inputs.PRODUCT_ID
SET tbl_Items.status = tbl_Inventory.status
WHERE tbl_Items.status <> tbl_Inventory.status
As you can see, I want to update tbl_Items to have a status matching its corresponding record in tbl_Inventory. The query should only be applied to rows were tbl_Items and tbl_Inventory disagree. Before I can access tbl_Inventory, I need to join with tbl_Inputs.
I found the solution, which was to use AS to create a nickname.
The below query works:
UPDATE tbl_Items
INNER JOIN
(SELECT *
FROM tbl_Inventory
INNER JOIN tbl_Inputs
ON tbl_Inventory.class = tbl_Inputs.class) AS nickname
ON tbl_Items.PRODUCT_ID = nickname.PRODUCT_ID
SET tbl_Items.status = nickname.status
WHERE tbl_Items.status <> nickname.status
I'm not 100% sure of Access syntax for this. However, in standard SQL, your statement should be written thus:
UPDATE tbl_items
SET tbl_items.status = tbl_Inventory.status
FROM tbl_items
INNER JOIN tbl_Inputs
ON tbl_items.PRODUCT_ID = tbl_Inputs.PRODUCT_ID
INNER JOIN tbl_Inventory
ON tbl_Inventory.class = tbl_Inputs.class
WHERE tbl_items.status <> tbl_Inventory.status;
Furthermore, the tbl_ prefix is unnecessary and bad practice. If #Celko were here he'd undoubtedly insist you remove it.

UPDATE query with multiple INNER JOINs (syntax error)

I'm trying to run an update query with multiple join statements in VBA, but I keep getting "missing operator" errors. Same happens when I try to run the SQL in query builder. The SQL is:
UPDATE TBL_DocReview_SingleStudy
INNER JOIN TBL_LOA
ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member
INNER JOIN TBL_STUDY
ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.Status
WHERE TBL_STUDY.CDB = 'B'
Can anyone see what I'm doing wrong?
Try:
UPDATE (TBL_DocReview_SingleStudy
INNER JOIN TBL_LOA
ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member)
INNER JOIN TBL_STUDY
ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.Status
WHERE TBL_STUDY.CDB = 'B'
Try the following:
UPDATE s
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.STATUS
FROM TBL_DocReview_SingleStudy S
INNER JOIN TBL_LOA ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member
INNER JOIN TBL_STUDY ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
WHERE TBL_STUDY.CDB = 'B'