SQL - Update table - sql-server-2008

I need to update a table and this is my sql code so far but i'm getting the following error message:
Line 30: Incorrect syntax near ')'.
UPDATE dbo.Part
SET SupplierShortName = NationalSupplier.ShortName,
SupplierLongName = NationalSupplier.LongName
SELECT *
FROM dbo.Part
JOIN dbo.NationalSupplier
ON Part.SupplierNumber = NationalSupplier.Number
AND (ISNULL(Part.SupplierShortName,'') <> ISNULL(NationalSupplier.ShortName,'')
OR ISNULL(Part.SupplierLongName,'') <> ISNULL(NationalSupplier.LongName,''))
LEFT OUTER JOIN dbo.NationalPart
ON Part.NationalPartID = NationalPart.NationalPartID
WHERE Part.DWCreationEntityID = 1
AND NationalPart.NationalPartID is NULL
Thanks in advance for any valuable tips !

Try this:
UPDATE dbo.Part
SET SupplierShortName = NationalSupplier.ShortName,
SupplierLongName = NationalSupplier.LongName
FROM dbo.Part
JOIN dbo.NationalSupplier
ON Part.SupplierNumber = NationalSupplier.Number
AND (ISNULL(Part.SupplierShortName,'') <> ISNULL(NationalSupplier.ShortName,'')
OR ISNULL(Part.SupplierLongName,'') <> ISNULL(NationalSupplier.LongName,''))
LEFT OUTER JOIN dbo.NationalPart
ON Part.NationalPartID = NationalPart.NationalPartID
WHERE Part.DWCreationEntityID = 1
AND NationalPart.NationalPartID is NULL
You should alias the table names to make things concise.

Related

SQL MS Access 2013

I have the following SQL statement:
UPDATE tbl_Invoices
SET tbl_Invoices.Base = tbl_Bases.BasePrice
FROM tbl_Invoices
INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber = tbl_Invoices.ProductNumber
AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID
AND tbl_Bases.CustomerName = 'VALEO'
AND tbl_Bases.CountryCode = 'FR'
AND tbl_Bases.ContractYear = 0
Access keep telling me I have a syntax error (missing operator)
This drives me nuts. Can someone tell me what's wrong?
UPDATE ... FROM doesn't exist in Access SQL, here you do it like this:
UPDATE tbl_Invoices
INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber = tbl_Invoices.ProductNumber
AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID
AND tbl_Bases.CustomerName = 'VALEO'
AND tbl_Bases.CountryCode = 'FR'
AND tbl_Bases.ContractYear = 0
SET tbl_Invoices.Base = tbl_Bases.BasePrice
Thanks a lot.
I still have an error as the = 'VALEO' etc... are not allowed in Join but this is working if moved to a where clause:
UPDATE tbl_Invoices
INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber = tbl_Invoices.ProductNumber
AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID
SET tbl_Invoices.Base = tbl_Bases.BasePrice
WHERE tbl_Bases.CustomerName = 'VALEO'
AND tbl_Bases.CountryCode = 'FR'
AND tbl_Bases.ContractYear = 0

UPDATE Query Giving me an SQL Error (1064)

I am trying to update a table in my database from another table. Here is my Syntax, but I can't seem to find any issues with it. I keep getting SQL Error (1064).
UPDATE customers b
SET customers.takerid = customer_update_2016.ot
FROM customer_update_2016 a, customers b
WHERE a.phone = b.phone && a.lname = b.lname
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM customer_update_2016 a, customers b WHERE a.phone = b.phone & a.lname =b' at line 3
Solution:
UPDATE customers
INNER JOIN customer_update_2016
ON customers.phone = customer_update_2016.phone
AND customers.lname = customer_update_2016.lname
SET customers.takerid = customer_update_2016.ot
you have both mysql and sql-server
which one?
UPDATE customers
SET customers.takerid = customer_update_2016.ot
FROM customers
JOIN customer_update_2016
on customers.phone = customer_update_2016.phone
and customers.lname = customer_update_2016.lname
and customers.takerid <> customer_update_2016.ot
Follow something like this always
UPDATE [table1_name] AS t1
INNER JOIN [table2_name] AS t2
ON t1.[column1_name] = t2.[column1_name]
SET t1.[column2_name] = t2.[column2_name];
To solve your problem you should use JOIN, in my case I had to pass data from one table to another like you and this was the way that solved my problem, hope it help with yours and others.(I'm using MariaDB v10.4)
UPDATE
table1
LEFT JOIN table2 AS tb2
ON tb2.fieldThatJoin = table1.fieldThatJoin//this part indicate the field that join them
SET
table1.field1 = tb2.field1;
//if you want to update more than one field then do this
table1.field1 = tb2.field1,
table1.field2 = tb2.field2,
table1.field3 = tb2.field3;//remember to put the ';' at the end

Updating table with joining id in MySQL

I’m trying to update a table that is joined with another one to update the right record.
Here is my command so far:
UPDATE
links l
SET
l.l_id = `[value-1]`,
l.l_facebook = `[value-2]`,
l.l_youtube = `[value-3]`,
l.l_twitter = `[value-4]`,
l.l_googleplus = `[value-5]`,
l.l_rss = `[value-6]`,
l.l_homepage = `[value-7]`,
l.l_freigegeben = `[value-8]`
JOIN
sponsering ON l.l_id = sponsering.links_l_id
WHERE
sponsering.s_userID = 2
Trying to run the command in phpmyadmin gives me the following error message:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'JOIN sponsering ON l.l_id = sponsering.links_l_id WHERE
sponsering.s_user' at line 12
I need to join the table sponsering because this gives me the correct record in the links table.
How can I solve this?
Try this
UPDATE
links l,
sponsering s
SET
l.l_faceook = `[value-2]`,
l.l_youtube = `[value-3]`,
l.l_twitter = `[value-4]`,
l.l_googleplus = `[value-5]`,
l.l_rss = `[value-6]`,
l.l_homepage = `[value-7]`,
l.l_freigegeben = `[value-8]`
WHERE
l.l_id = s.links_l_id AND
sponsering.s_userID = 2
In MySQL, the join is part of the update statement itself. There is no separate from:
UPDATE links l JOIN
sponsering s
ON l.l_id = s.links_l_id
SET
l.l_id = `[value-1]`,
l.l_faceook = `[value-2]`,
l.l_youtube = `[value-3]`,
l.l_twitter = `[value-4]`,
l.l_googleplus = `[value-5]`,
l.l_rss = `[value-6]`,
l.l_homepage = `[value-7]`,
l.l_freigegeben = `[value-8]`
WHERE s.s_userID = 2;
Some other databases use a FROM clause for the same purpose.

syntax error when running query in MS Access 2010

good day
i am trying to run an update query but getting the following error "syntax error in join operation"
Here is the query:
update (table 1
set SEQ=table2.SEQ)
from (table1 inner join table2 on table1.NBR=table2.NBR and table1.LINE = table2.LINE and table1.VENNO = table2.VENNO and table1.INVNO = table2.INVNO where table1.SEQ <> table2.seq)
Any assistance will be appreciated.
Your syntax is wrong. UPDATE queries don't use FROM. The right syntax is:
update
table1
inner join table2
on table1.NBR=table2.NBR
and table1.LINE = table2.LINE
and table1.VENNO = table2.VENNO
and table1.INVNO = table2.INVNO
set
table1.SEQ = table2.SEQ
where
table1.SEQ <> table2.seq

Multiply in update subquery select

I'm trying to update a table based on 2 select subquery that will be multipied to produce the value for Harga column
here is my code :
UPDATE bahanmakanan
SET Harga = (SELECT HargaSatuan from detail_bahanmakanan
WHERE IDBahanMakanan = "BM01")* (SELECT jumlah from bahanmakanan
WHERE IDBahanMakanan = "BM01")
WHERE IDBahanMakanan = "BM01" ;
The error message return
Error Code: 1093. You can't specify target table 'bahanmakanan' for update in FROM clause
you can simply do this using JOIN,
UPDATE bahanmakanan a
INNER JOIN detail_bahanmakanan b
ON a.IDBahanMakanan = b.IDBahanMakanan
SET a.Harga = a.jumlah * b.HargaSatuan
WHERE a.IDBahanMakanan = 'BM01'
Please do backup first your database before executing the statement.
Try this:
UPDATE bahanmakanan as t1
JOIN detail_bahanmakanan as t2 USING(IDBahanMakanan)
SET t1.Harga = t2.HargaSatuan * t1.jumlah
WHERE IDBahanMakanan = "BM01";