Update Query from a Lookup Query - sql

I have a spreadsheet that I am converting to an Access DB. I have a column of typed out customer names that I want to replace with the appropriate customer number from our accounting system.
I have created a table with the customer info, and a query that shows what ID needs to be inserted into the source data. What I'm looking for is:
UPDATE tblStarting_Data
SET CustomerID=x
WHERE TEMPCustomer=y
Where X and Y come from qryIDPerCustomer.
Can I use a loop? How do I reference another query?

Another possibility in MS Access (object names borrowed from Tomalak answer):
UPDATE tblStarting_Data, qryIDPerCustomer
SET tblStarting_Data.CustomerID=qryIDPerCustomer.CustomerID
WHERE tblStarting_Data.TEMPCustomer=qryIDPerCustomer.CustomerName

I think a JOIN will help you:
UPDATE
tblStarting_Data AS sd
INNER JOIN qryIDPerCustomer AS qc ON sd.TEMPCustomer = qc.CustomerName
SET
sd.CustomerID = qc.CustomerID;
This can be expressed as a correlated sub-query as well (though the join syntax is preferable):
UPDATE
tblStarting_Data
SET
CustomerID = (
SELECT CustomerID
FROM qryIDPerCustomer
WHERE CustomerName = tblStarting_Data.TEMPCustomer
)
No need for a loop, both statements will update all records in tblStarting_Data in one step.

Related

Insert Into where not exists from specific category

I have a table that contains several repair categories, and items that are associated with each repair category. I am trying to insert all the standard items from a specific repair category that don't already exist into a Details table.
TblEstimateDetails is a join table for an Estimate Table and StandardItem Table. And TblCategoryItems is a join table for the Repair Categories and their respective Standard Items.
For example in the attached image, Left side are all the Standard Items in a Repair Category, and Right side are all the Standard Items that are already in the EstimateDetails table.
Standard Items All vs Already Included
I need to be able to insert the 6 missing GUIDS from the left, and into the table on the right, and only for a specific estimate GID.
This is being used in an Access VBA script, which I will translate into the appropriate code once I get the sql syntax correct.
Thank you!
INSERT INTO TblEstimateDetails(estimate_GID, standard_item_GID)
SELECT
'55DEEE29-7B79-4830-909C-E59E831F4297' AS estimate_GID
, standard_item_GID
FROM TblCategoryItems
WHERE repair_category_GID = '32A8AE6D-A512-4868-8E1A-EF0357AB100E'
AND NOT EXISTS
(SELECT standard_item_GID
FROM TblEstimateDetails
WHERE estimate_GID = '55DEEE29-7B79-4830-909C-E59E831F4297');
Some things to try: 1) simplify to a select query to see if it selects the right records, 2) use a NOT IN statement instead of NOT EXISTS. There's no reason NOT EXISTS shouldn't work, I'd just try a different way if it isn't working.
SELECT '55DEEE29-7B79-4830-909C-E59E831F4297' AS estimate_GID,
standard_item_GID
FROM TblCategoryItems
WHERE repair_category_GID = '32A8AE6D-A512-4868-8E1A-EF0357AB100E'
AND standard_item_GID NOT IN
(SELECT standard_item_GID FROM TblEstimateDetails
WHERE estimate_GID = '55DEEE29-7B79-4830-909C-E59E831F4297');
Got it figured out. Access needs the subquery to be correlated to main query to work. So I set the WHERE clause in the subquery to equal the matching column in the main query. And I had to join the Estimates table so that it picked only the items in a specific estimate.
SELECT
'06A2E0A9-9AE5-4073-A856-1CCE6D9C48BB' AS estimate_GID
, CI.standard_item_GID
FROM TblCategoryItems CI
INNER JOIN TblEstimates E ON CI.repair_category_GID=E.repair_category_GID
WHERE E.repair_category_GID = '15238097-305E-4456-B86F-3787C9B8219B'
AND NOT EXISTS
(SELECT ED.standard_item_GID
FROM TblEstimateDetails ED
WHERE E.estimate_GID=ED.estimate_GID
);

Cannot update a table using a simple inner join

I have 2 tables in access 2007.
See attached picture to see the structure of the tables and the expected result.
I am trying to update the quantity field (ITQTY) in TABLE_BLNC by summarizing all the quantity field (LOCQTY) from TABLE_DTL for same items (LOITNBR=ITNBR).
In TABLE_BLNC, the item is unique while in TABLE_DTL, the item can be in multiple records.
My query is:
UPDATE TABLE_BLNC INNER JOIN
(
SELECT LOITNBR, Sum(LOCQTY) AS SumOfLOCQTY FROM TABLE_DTL GROUP BY LOITNBR) AS DTL
ON TABLE_BLNC.ITNBR=DTL.LOITNBR SET TABLE_BLNC.ITQTY = DTL.SumOfLOCQTY;
I am getting the error:
Operation must use an updateable query.
Domain Aggregate functions can be useful when Access complains that an UPDATE is not updateable. In this case, use DSum() ...
UPDATE TABLE_BLNC
SET ITQTY =
DSum("LOCQTY", "TABLE_DTL", "LOITNBR='" & ITNBR & "'");
Index TABLE_DTL.LOITNBR for optimum performance.
One of the great annoyances of Access SQL is its inability to update a table from an non-updatable source. Non-updatable sources include read-only links to ODBC tables, and GROUP BY (summary) queries.
What I always do is:
Copy the structure of TABLE_BLNK to a temp table: TABLE_BLNK_temp.
In your code, first delete the temp:
DELETE * FROM TABLE_BLNK_temp;
Insert the result of your summary query into temp:
INSERT INTO TABLE_BLNK_temp (ITNBR, ITQTY)
SELECT LOITNBR, Sum(LOCQTY) AS SumOfLOCQTY
FROM TABLE_DTL GROUP BY LOITNBR;
Update TABLE_BLNK from TABLE_BLNK_temp:
UPDATE TABLE_BLNC INNER JOIN TABLE_BLNK_temp AS t
ON TABLE_BLNC.ITNBR = t.ITNBR
SET TABLE_BLNC.ITQTY = t.ITQTY;
While it is an extra step or two, this approach:
Always works
Is more performant than Domain Aggregate functions for larger datasets

Update query based on two conditions

I have the following table based on this query:
SELECT
repName.repID, repName.Rep_Name, repName.Job_Code, GenItems.Item_Name,
repName.Entered
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
ORDER BY
repName.Rep_Name
I want to add an update routine to it. I want to update the entered field if the user entry matches the rep.ID and the Item Name. and finally return the Max value for the Entered field. Can I add this to this query or is it better to write another.
I just started working with sql, so if my questions seems basic, please forgive me. I am self taught and stumbling greatly.
Thank you
I don't understand fully your question.
You are showing us a SELECT statement. It can only be used to return a table-like result. If you want to upate a table you must use an UPDATE query. For the SQL-Server (and SQL CE) the query looks like this:
UPDATE repName
SET repName.Entered = x
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
WHERE
repName.repID = x AND GenItems.Item_Name = 'y'
The difficulty is that tables have to be joined in the UPDATE statement. This not supported in Oracle for instance, where you have to do it with sub-selects.

Updated SQL Field based on Conditions in a Related Table

I am new to SQL and despite hours of searching, cannot figure out the SQL query to update records in my members table, based on conditions in my payments table. I'm very confused whether I use a JOIN (and if so what kind) or a Subquery?
Here's what I have so far:
UPDATE wp_mcra_members
SET wp_mcra_members.dues_paid = 1
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
WHERE wp_mcra_payments.year_paid = '2013' and wp_mcra_payments.reason = 'Dues';
I want the databased to search for any records in the Payments table that meet my conditions of being year 2013 and labeled Dues. Then I want the Members table to update the field dues_paid based on any found records matching those conditions, where the Member ID = Payments Member
The syntax for an update with a join varies by database. Here is generic syntax using a subquery in the where clause:
UPDATE wp_mcra_members
SET dues_paid = 1
where wp_mcra_members.id in (select wp_mcra_payments.member
from wp_mcra_payments
WHERE wp_mcra_payments.year_paid = '2013' and
wp_mcra_payments.reason = 'Dues'
) ;
The syntax you supplied looks like it would work if you are using SQL Server. Oracle does not support JOINs with UPDATE, instead look into using MERGE or Gordon's answer would also work there.
Assuming however you are using MySQL, a JOIN in the UPDATE statement would probably have a better performance than using IN:
UPDATE wp_mcra_members
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
SET wp_mcra_members.dues_paid = 1
WHERE wp_mcra_payments.year_paid = '2013'
and wp_mcra_payments.reason = 'Dues';

Multiple Query Writing in Single Query

I have two tables in Database , I need to select a field from one table and update it in another table with a condition where id is same .. Is it Possible to write in single query ???
This should work for you:
update storage
set storage.email = (select register.email
from register
where register.id = storage.id)
Yeah it is, you could do this for example:
UPDATE Origin SET DesiredColumn = NewValue
FROM Origin
JOIN NewTable ON Origin.Id = NewTable.Id
And guess the column names were like DesiredColumn in the updating table and NewValue in the table that holds the new value.
Yes, this is possible, although the syntax depends on the type of SQL you are using.
Here is an example for T-SQL (for Microsoft SQL Server)
UPDATE
S
SET
Email = R.Email
FROM
dbo.Register R
INNER JOIN dbo.Storage S
ON S.RegisterID = R.RegisterID