Access query: Update with a DSum in SET - sql

I get the error "Operation must use an updateable query" when I try to run this SQL code:
UPDATE Progetti
SET Progetti.Eroso = (SELECT sum(Fatture.Fattura) FROM Fatture WHERE Fatture.[Codice Progetto] = Progetti.[Codice Progetto]);
Consider that all the tables, fields and relationships involved exist and are correctly set. The issue (according to me) is that SELECT cannot be inside SET. Is it correct?
So what can be a right solution?
The result must be: for each Progetti.[Codice Progetto] put in the field Progetti.Eroso the sum only of Fatture.Fattura related to Progetti.[Codice Progetto].
An alternative can be:
UPDATE Progetti AS a
SET a.Eroso = DSum("Fattura", "Fatture", "[Codice Progetto]=" & a.[Codice Progetto]);
But I get this warning:
EDIT
I have tried the solution of #user4321:
UPDATE Progetti
SET Progetti.Eroso = Fatture2.FatturaSum
FROM Progetti
Inner Join (SELECT sum(Fatture.Fattura) as FatturaSum
FROM Fatture) as Fatture2
ON Fatture2.[Codice Progetto] = Progetti.[Codice Progetto];
Maybe I have found why it does not work: Fatture.[Codice Progetto] is probably linked to Progetti.[ID Progetto] (that is different from Progetti.[ID Progetto]). The field is set in this way:

hope this works for you
UPDATE Progetti
SET Progetti.Eroso = Fatture2.FatturaSum
FROM Progetti
Inner Join (SELECT sum(Fatture.Fattura) as FatturaSum
FROM Fatture) as Fatture2
ON Fatture2.[Codice Progetto] = Progetti.[Codice Progetto];

Related

Problem in PostgreSQL (ERROR: relation "a" does not exist)

SQL query (PostgreSQL) looks like that:
UPDATE a
SET "PropertyAddress" = COALESCE(a."PropertyAddress", b."PropertyAddress")
FROM "NashvilleHousingData" a
INNER JOIN "NashvilleHousingData" b
ON a."ParcelID" = b."ParcelID"
AND a."UniqueID" <> b."UniqueID"
WHERE a."PropertyAddress" IS NULL;
And the error is relation "a" does not exist
I tried other advices about the notion in the code public or scheme, but it still doesn't work. Please, help
This is not how Postgres handles updates with more than one table. You don't repeat the table in the from clause:
UPDATE "NashvilleHousingData" nhd
SET "PropertyAddress" = COALESCE(nhd."PropertyAddress", nhd2."PropertyAddress")
FROM "NashvilleHousingData" nhd2
WHERE nhd2."ParcelID" = nhd."ParcelID" AND
nhd2."UniqueID" <> nhd."UniqueID"
WHERE nhd."PropertyAddress" IS NULL;
Also, the COALESCE() is superfluous because the value is known to be NULL:
SET "PropertyAddress" = nhd2."PropertyAddress"

Update a Table using a Join

I wish to update a table using, but need to use another table to get the correct field. The new information is not taken from another field from another table.
The following SQL statement returns the correct information:
SELECT PURCHASEHEADER.ORDERNOTES
FROM PURCHASEHEADER, ASSEMBLYLINESOURCE
WHERE ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
AND PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
I have tried the following:
UPDATE PURCHASEHEADER SET PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (
SELECT 1 FROM ASSEMBLYLINESOURCE
WHERE PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
) AND ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
An error is returned saying: " ...Column Unknown ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID..." but it does exist as it works in the first query.
I have seen similar posts from Mark Rotteveel dated July 2017, but still can't get it to work.
There is an issue with your closing bracket. Try this, it worked for me.
UPDATE PURCHASEHEADER set PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (SELECT 1 FROM ASSEMBLYLINESOURCE WHERE
PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER AND
ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001)

trying to update a row from one table to another

UPDATE customer_service.batch_status.cust_nm
SET batch_status.cust_nm
SELECT customer_service.batch_detail.cust_nm
FROM customer_service.batch_detail
LEFT JOIN customer_service.batch_status
ON customer_service.batch_detail.cust_nm = customer_service.batch_status.cust_nm;
Your syntax is off.
Instead:
UPDATE customer_service.batch_status.cust_nm
FROM customer_service.batch_detail csbd
SET cust_nm = csbd.cust_nm
WHERE csbd.cust_nm = customer_service.batch_status.cust_nm;

How to update columns in a table joined with other tables?

I want to update two columns of a table with reference of other tables. While executing the script its showing error.
Error: Error starting at line 1 in command:
UPDATE wb_costing_work_items,
sa_sales_documents,
sa_sales_document_items
SET cwi_price_per_hour = sdi_price,
cwi_amount = sdi_price * cwi_hours
WHERE cwi_lo_id = sad_lo_id
AND sdi_sad_id = sad_id
AND sdi_wit_id = cwi_wit_id
AND cwi_id = 1650833
Error at Command Line:1 Column:28 Error report: SQL Error: ORA-00971:
missing SET keyword
00971. 00000 - "missing SET keyword"
SQL STATEMENT
UPDATE wb_costing_work_items cwi,
sa_sales_documents sad,
sa_sales_document_items sdi
SET cwi.cwi_price_per_hour = sdi.sdi_price,
cwi.cwi_amount = sdi.sdi_price * cwi.cwi_hours
WHERE cwi.cwi_lo_id = sad.sad_lo_id
AND sdi.sdi_sad_id = sad.sad_id
AND sdi.sdi_wit_id = cwi.cwi_wit_id
AND cwi.cwi_id = 1650855
This should definitely work.
UPDATE (SELECT cwi_price_per_hour,
sdi_price,
cwi_amount,
sdi_price,
cwi_hours
FROM wb_costing_work_items,
sa_sales_documents,
sa_sales_document_items
WHERE cwi_lo_id = sad_lo_id
AND sdi_sad_id = sad_id
AND sdi_wit_id = cwi_wit_id
AND cwi_id = 1650833)
SET cwi_price_per_hour = sdi_price, cwi_amount = sdi_price * cwi_hours
Please alias the tables used and prefix columns so one can easily read your query.
Something like this maybe.
Note that I had to use some wild guesses about which column belongs to which table as you have not included any information about how your tables are define.
So most probably I didn't get it right and you will need to adjust the query to your actual table structure.
merge into wb_costing_work_items
using
(
select cwi.pk_column, -- don't know what the PK of the wb_costing_work_items is due to lack of information
sdi.sdi_price, -- don't know if this column really belong to this table
sdi.sdi_price * cwi.cwi_hours as total-- again unsure about column names due to lack of information
FROM wb_costing_work_items cwi
JOIN sa_sales_documents sad ON sad.sad_lo_id = cwi.cwi_lo_id -- not sure about these, due to lack of information
JOIN sa_sales_document_items sdi
ON sdi.sad_id = sad.sdi_sad_id -- not sure about these, due to lack of information
AND sdi.sdi_wit_id = cwi.cwi_wit_id -- not sure about these, due to lack of information
) t ON (t.pk_column = w.pk_column) -- not sure about this, due to lack of information
when matched then
update
set cwi_price_per_hour = t.sdi_price,
cwi_amount = t.total;

Selects in Joins

I have query
UPDATE THD
SET RepostFlag = 'Y'
,RunListNoRetroPolicyPrepay = ?
,RetroObject = ?
FROM TranHead AS THD
JOIN (
SELECT CustPolicyNo AS CustPolicyNo
,MIN(PremPeriod) AS PremPeriod
FROM TranHead
WHERE RepostFlag = 'Y'
AND PayoutTypeNo = ?
GROUP BY CustPolicyNo
) AS THDToBeReposted ON THD.CustPolicyNo = THDToBeReposted.CustPolicyNo
WHERE THD.RepostFlag = 'N'
AND THD.PremPeriod > THDToBeReposted.PremPeriod
fails in H2 with following message
Table "THD" not found;
I looked at http://www.h2database.com/html/grammar.html#table_expression to see if H2 supports selects in join. It appears it does. Maybe I am missing something when looking at the grammar, but it seems to me that the query should work in H2.
Anyone see what is wrong?
Thanks.
I don't believe FROM is allowed in the UPDATE syntax.
You can't update an alias, you need to have the table name specified.
Complementary to other answers, JOIN (just as FROM) is not allowed in UPDATE for H2. It would be allowed in a sub query.
Essentially, stick to the basic syntax:
UPDATE SomeTable as SomeAlias
SET SomeField = ?
WHERE (%GoWild%)
Whether or not you need the alias is up to your where clause.
Reference: http://www.h2database.com/html/grammar.html#update