SQL Update statement: OperationalError near "FROM" - sql

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 = ?);

Related

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.

How to transform SQL query into JPQL query?

I'm looking for how to transform the following query (SQL) into query JPQL;
SELECT * FROM equipements eq
LEFT JOIN check_lists checks
ON eq.id_equipements = checks.equipements_id
LEFT JOIN responses_check_lists resp
ON checks.id_check_lists = resp.check_lists_id
AND resp.missions_id = 15
AND eq.id_equipements = 1
ORDER BY checks.id_check_lists
I followed the documents on the internet but I do not get the correct transformation of my query in JPQL.
I know that the attributes of the query will be replaced by the attributes of the class.
I posted here to help me in transforming the SQL query.
Edit1:
#Query(
"SELECT r, checks, eq"
+ " FROM Equipements eq"
+ " LEFT JOIN CheckLists checks "
+ " ON eq.idEquipements = checks.equipements.idEquipements"
+ " LEFT JOIN ResponsesCheckLists r"
+ " ON checks.idCheckLists = r.CheckLts.idCheckLists"
+ " AND r.Respmission.idMission= :idmiss "
+ " AND eq.idEquipements= :idEqp"
+ " ORDER BY checks.idCheckLists ASC"
)
Error of Edit1:
Caused by: java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'r' {originalText=r}
org.hibernate.hql.internal.ast.InvalidPathException: Invalid path:
'checks.idCheckLists'
Thank you in advance,
Analysing your SQL and without see the entities, your JPQL can be something like this:
SELECT eq FROM Equipements eq
LEFT JOIN eq.checks check
LEFT JOIN check.responses resp
LEFT JOIN resp.missions mission WITH mission.id = :idmiss
WHERE eq.id = :idEqp
ORDER BY check.id
But, to be something like this, you need to adjust the mapping of your entities (entity name, column names, etc).

SQL Statement won't work anymore after small change. VBA

this Statement works totaly fine in ORACLE SQL DEVELOPER, but when I insert it to my vba makro it won't return me my BLOB files from the database. The statement worked before in VBA with a data-number variable instead of referencing to SYSDATE...
Provider is OraOLEDB.Oracle, the Connections string "works/opens" but there is no data to pass my - IF rs.EOF = FALSE Then - I've already tested eof=true but no data comes through....Any ideas ? thx!
select datas
from tdb
INNER JOIN (select datas_id
from tfdz
INNER JOIN (select fb_id
from tfb
INNER JOIN (select pu_id
from tpu
INNER JOIN (select tap.p_id
from tap
INNER JOIN (SELECT po_id
FROM tpo
WHERE tpo.lastdate like (SYSDATE-1)
) sub_tpo
ON tap.po_id = sub_tpo.po_id and tap.lastdate like (SYSDATE-1)
) sub_tap
ON tpu.p_id = sub_tap.p_id
) sub_tpu
ON tfb.pu_id = sub_tpu.pu_id
where tfb.deleated = 0
) sub_tfb
ON tfdz.fb_id = sub_tfb.fb_id
) sub_tfdz
ON tdb.datas_id = sub_tfdz.datas_id
order by sub_tfdz.datas_id asc
It would be good to see how you are passing it, I assume as a string, have you tried.
WHERE tpo.lastdate like (" & Format(DateAdd("d", -1, Now()), "DD/MMM/YYYY") & ")"
You may need to change the format to be compatible Oracle date processing

SQL update syntax with subquery

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.

date time in combobox

i am using visual stdio 2008 and sql server 2005
dim selectquery = "SELECT Purchase_master.Customer_name, Purchase_details.Item_code, Item_Master.Name,
Purchase_details.Quantity, Purchase_details.Cost, Purchase_master.Date
FROM Item_Master INNER JOIN (Purchase_master INNER JOIN Purchase_details ON
Purchase_master.Bill_id = Purchase_details.Bill_id) ON Item_Master.Item_code = Purchase_details.Item_code
WHERE Purchase_master.Date= " + cboPDate.SelectedValue.ToString()
when this selectquery executed it gives me
error
"ERROR near syntax 12"
my cboPDate is a combobox binded with my database which return's data and time in
"2/18/2011 12:00:00 AM"
please help me out
You need to add quotes around your date.
Try
dim selectquery = "SELECT Purchase_master.Customer_name, Purchase_details.Item_code, Item_Master.Name,
Purchase_details.Quantity, Purchase_details.Cost, Purchase_master.Date
FROM Item_Master INNER JOIN (Purchase_master INNER JOIN Purchase_details ON
Purchase_master.Bill_id = Purchase_details.Bill_id) ON Item_Master.Item_code = Purchase_details.Item_code
WHERE Purchase_master.Date= '" + cboPDate.SelectedValue.ToString() +"'"
Better yet, use SQL parameters.
I suspect it is treating it as a delimiter.It would be better to change it to
WHERE Purchase_master.Date=#Your_date
And then add the date as a parameter, this would prevent SQL injection attacks and also promote plan caching
Try this:
dim selectquery = string.Format("SELECT Purchase_master.Customer_name, Purchase_details.Item_code, Item_Master.Name,
Purchase_details.Quantity, Purchase_details.Cost, Purchase_master.Date
FROM Item_Master INNER JOIN (Purchase_master INNER JOIN Purchase_details ON
Purchase_master.Bill_id = Purchase_details.Bill_id) ON Item_Master.Item_code = Purchase_details.Item_code
WHERE Purchase_master.Date= '{0}'", cboPDate.SelectedValue.ToString());
you have to add quots to the value. :)