SQL update syntax with subquery - sql

I have this update statement:
UPDATE quotedetailextensionbase qd
SET qd.new_capvehicleid = (SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype)
I get an error back but I'm not sure why
Incorrect syntax near 'qd'

Seems like it would be easier to achieve this result with an update-join statement:
UPDATE qd
SET new_capvehicleid = cv.new_capvehicleid
FROM quotedetailextensionbase qd
JOIN (SELECT new_capvehicleid
FROM vwCapidLookup) cv ON cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype

SQL Server lets you do joined updates:
UPDATE
qd
SET
new_capvehicleid = cv.new_capvehicleid
FROM
quotedetailextensionbase qd
join vwCapidLookup cv on
cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype
;
Does that make sense?

Please try the below SQL:
UPDATE quotedetailextensionbase
SET new_capvehicleid =
(SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype)

Use MERGE: https://msdn.microsoft.com/en-us/library/bb510625.aspx. That is correct when you would update from another table.

Related

SQL Update statement: OperationalError near "FROM"

Replacing box = with tb.box = shifts the error to the '.':
query = (f'UPDATE tb '
f"SET box = '{box_update}' "
f'FROM {table} tb '
'INNER JOIN question qu ON qu.id = tb.question_id '
f'WHERE qu.number_a = {num_a} AND qu.number_b = {num_b};')
Error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "FROM": syntax error [SQL: "UPDATE tb SET box = '0' FROM addition tb INNER JOIN question qu ON qu.id = tb.question_id WHERE qu.number_a = 1 AND qu.number_b = 9;"]
Actual implementation may not be fully adhered even with latest SQLite to support UPDATE-FROM. Specifically, docs do not indicate:
JOIN in outer query is supported in UPDATE.
FROM table should not repeat table in UPDATE.
Table alias alone in UPDATE may not be allowed. Possibly no alias for updated table should be used.
Consider below adjustment aligned to example in docs. Below demonstrates parameterization with sqlite3 raw cursor. Adjust to however you run with sqlalchemy.
q = f'''UPDATE {table}
SET box = ?
FROM question qu
WHERE qu.id = {table}.question_id
AND qu.number_a = ?
AND qu.number_b = ?;
'''
cursor.execute(q, (box_update, num_a, num_b))
conn.commit()
It looks like the update syntax you are using either is not supported at all, or at least is not supported on your version of SQLite. However, you may rewrite your update to use exists logic:
query = (f'UPDATE {table} tb '
f"SET box = '{box_update}' "
f'WHERE EXISTS (SELECT 1 FROM question qu '
f' WHERE qu.id = tb.question_id AND '
f' qu.number_a = {num_a} AND qu.number_b = {num_b});')
The raw SQL query would look something like this:
UPDATE yourTable tb
SET box = ?
WHERE EXISTS (SELECT 1 FROM question qu
WHERE qu.id = tb.question_id AND
qu.number_a = ? AND qu.number_b = ?);

Can't figure out how to add current date to comment

I'm having problems with my sql code, i'm trying to implement a current date to my comment but i can't figure out how + i'm having syntax errors and i don't know what to do anymore. Can someone help me with the date adding to comment?
UPDATE Osalus_projektis AS op
SET op.töötasu = op.töötasu + 100,
op.comment = CONCAT(NOW()'tõusis palk 100 eurot')
FROM Osalus_projektis
INNER JOIN Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
WHERE ol.nimetus = 'nõustaja';
MS Access uses & for string concatenation. And it doesn't support a FROM clause. This may do what you want:
UPDATE Osalus_projektis AS op INNER JOIN
Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
SET op.töötasu = op.töötasu + 100,
op.comment = NOW() & 'tõusis palk 100 eurot'
WHERE ol.nimetus = 'nõustaja';
If there is a problem with the JOIN -- which happens a lot in MS Access -- then you can use an EXISTS clause as well.

SQL Update statement not working - SQL Server

I am trying to run the below sql statement (SQL Server), however getting the error
"FROM clause in UPDATE and DELETE statements cannot contain subquery sources or joins."
update fp
set fp.totalcapacity = hc.totalcapacity,
fp.sellablecapacity = hc.sellablecapacity
from [fact].[FinalPosition] fp
join fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey
and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18'
I can't seem to understand why I am getting this error. Any idea?
I think the syntax you want is:
update fp
set totalcapacity = hc.totalcapacity,
sellablecapacity = hc.sellablecapacity
from fp join
fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18';
If you want fp to refer to an actual table, include that in the from clause and make the fp the alias for the table.

Update using Sum -Error

Keep on getting error
you tried to execute a query that does not include the specified expression
My SQL looks as follows:
UPDATE TblField LEFT JOIN TblTempStats ON TblField.DomainCatID = TblTempStats.DomainCatID
SET TblTempStats.EmptyFields = Sum(IIf([fieldname] Is Null,1,0));
Any ideas as to why?
You should use a domain aggregate for this in my opinion, it avoids the error:
UPDATE TblTempStats
SET TblTempStats.EmptyFields =
DSum(
"IIf([fieldname] Is Null,1,0)",
"TblField",
IIf(
TblTempStats.DomainCatID Is Null,
"TblField.DomainCatID Is Null",
"TblField.DomainCatID = " & TblTempStats.DomainCatID
)
)

Updating a Microsoft Access 2013 table with data from a pass-through query

I am trying, unsuccessfully so far, to update records in a Microsoft Access 2013 table (called tbl_Data) with data from an AS400 table (LIBRARY.TABLE).
As you can see in my Access 2013 pass-through query below, I am trying to join the access table with the AS400 table using the Prefix & Number fields, and from there, update the access table with Name & Address information from the AS400 table.
Here is my latest attempt:
UPDATE
tbl_Data
SET
tbl_Data.FirstName = a.NINMFR,
tbl_Data.MiddleName = a.NINMMD,
tbl_Data.LastName = a.NINAML,
tbl_Data.BuildingNumber = a.NIBLNR,
tbl_Data.StreetName = a.NISTNM,
tbl_Data.AptSuite = a."NIAPT#",
tbl_Data.Address2 = a.NIADR2,
tbl_Data.City = a.NICITY,
tbl_Data.State = a.NISTAT,
tbl_Data.ZipCode = a.NIZIPC
INNER JOIN
LIBRARY.TABLE a
ON
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR;
When I run this query, I get an error that says:
OBDC--call failed.
[IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0199 - Keyword INNER not expected. Valid tokens: USE SKIP WAIT WITH WHERE. (#-199)
I would really appreciate any assistance, as I'm out of ideas.
Thanks!
That is Microsoft specific syntax for an update, it does not work on DB2. Try this:
UPDATE
tbl_Data
SET
(tbl_Data.FirstName,
tbl_Data.MiddleName,
tbl_Data.LastName,
tbl_Data.BuildingNumber,
tbl_Data.StreetName,
tbl_Data.AptSuite,
tbl_Data.Address2,
tbl_Data.City,
tbl_Data.State,
tbl_Data.ZipCode)
=
(SELECT
a.NINMFR,
a.NINMMD,
a.NINAML,
a.NIBLNR,
a.NISTNM,
a."NIAPT#",
a.NIADR2,
a.NICITY,
a.NISTAT,
a.NIZIPC
FROM
library.table a
WHERE
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR)
WHERE
EXISTS (
SELECT *
FROM
library.table a
WHERE
tbl_Data.Prefix = a.NIPRFX,
tbl_Data.Number = a.NIPLNR);