I'm trying to update an MS Access table by joining it to another table in another database and it's not working.
Here is the code I used:
UPDATE tbl_a a
INNER JOIN tbl_a b
IN '' [MS Access;PWD=Cb4XTNLq34c$;DATABASE=C:\data\memberdetails.mdb]
ON a.mobile=b.mobile
SET a.Mobilenew = b.Mobilenew,
a.isUpdated = 1,
a.Operator = b.Operator
WHERE b.isupdated=1
Can anyone see what I'm doing wrong?
What you should do is join the table to the database you're using. To do that, you should:
Click on "External Data" menu item on the top of Access
Click the Access icon
You're given a choice of importing or linking data. Choose "Link"
Browse and select the database containing the data you want to link (in your case, C:\data\memberdetails.mdb
You will be presented with a list of tables in the database. Choose which table(s) you want, and click OK
The new table will now exist in your database as a linked table. At that point, you can change your query to:
UPDATE tbl_a a
INNER JOIN tbl_a b
ON a.mobile=b.mobile
SET a.Mobilenew = b.Mobilenew,
a.isUpdated = 1,
a.Operator = b.Operator
WHERE b.isupdated=1
Since they're linked, any change that's made to tbl_a in either database will effect both databases, so just keep that in mind which you're working with it.
Related
I'm having issues where I'm unable to update any fields in my form.
I'll get "This recordset is not updatable" when I try to modify some fields.
I had to working when I inner joined 2 tables on my Form's Record Source.
SELECT dbo_Menzits.*, dbo_CCC_Job.*
FROM dbo_Menzits
INNER JOIN dbo_CCC_Job ON dbo_Menzits.MenzitID = dbo_CCC_Job.ExternalId
However when i try to join another table it won't allow me to update the recordset (as shown below). I know Access has some rules that needs to be followed as specified Here but I'm unable to understand some of the rules.
All my tables are linked tables and they all have primary keys.
This is how i joined it:
SELECT dbo_Menzits.*, dbo_CCC_Job.*, dbo_jms_JobDetails.*
FROM (dbo_Menzits
INNER JOIN dbo_jms_JobDetails ON dbo_Menzits.MenzitID = dbo_jms_JobDetails.MenzitID)
INNER JOIN dbo_CCC_Job ON dbo_Menzits.MenzitID = dbo_CCC_Job.ExternalId
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
);
UPDATE tblInstance
(INNER JOIN Master_Table ON tblInstance.[WorkOrder] = Master_Table.[Work_Order_number])
INNER JOIN tblCustomer_New ON Master_Table.[Customer_Name] = tblCustomer_New.[Customer_Name]
SET tblInstance.to_test_date = [Master_Table].[tblCustomer_New.Inital_Invoice];
I use a record from one table to choose a field in another table to update a record in a third table. I have all the table relationships set within Access.
Edit:
A value box pops up when trying to run it. It isn't pulling any data from the master table.
When MS Access does not find a certain identifier in your query it shows a value box asking for the value. See: Why does Access want me to enter a parameter value?
To solve this you can take the following steps:
Look at the parameter name it is asking for; it could be that the column name or a table name is incorrectly written. If you see an incorrect name which is not in the database, fix it.
Otherwise try the same query but as a select statement:
SELECT *
FROM tblInstance
(INNER JOIN Master_Table ON tblInstance.[WorkOrder] = Master_Table.[Work_Order_number])
INNER JOIN tblCustomer_New ON Master_Table.[Customer_Name] = tblCustomer_New.[Customer_Name];
And then try just the subqueries, starting with, for example:
SELECT *
FROM tblInstance
(INNER JOIN Master_Table ON tblInstance.[WorkOrder] = Master_Table.[Work_Order_number])
So at work we've been using Oracle SQL Developer 4.1.3.20 lately to query our data so I wrote this snippet of code (its actually much longer by 8 or so more table joins):
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from xyz.DRUG_KIT k
left join xyz.DR_SHIP s on s.ID = k.SHIPMENT_ID
left join xyz.USR_PAT u on u.PAT_ID = k.PAT_ID
When I have to switch to another table in a different database, I have been copy pasting everywhere above where you see 'xyz' but surely there has to ba better way? I use to use SQL Server Management Studio 2014 but we had to switch to this once we began using oracle databases. How would I declare a varchar(?) variable in Oracle SQL Developer so I can just use that one variable in place of XYZ and at the top of the query I just can set that variable equal whatever necessary in one place if that makes sense. It becomes cumbersome when I copy paste something wrong on one line when I"m trying to join about 10+ tables together to retrieve data.
XYZ is the owner of the table, not a different database. If you are connected as user XYZ, then you don't need to prefix XYZ to the table name, it's only required if you are attempting to access objects owned by a different "SCHEMA".
You can change the current "parsing" schema of your session by issuing an alter session command:
ALTER SESSION SET CURRENT_SCHEMA=PDQ;
and then select from objects the user PDQ has granted you select privs on:
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from DRUG_KIT k
left join DR_SHIP s on s.ID = k.SHIPMENT_ID
left join USR_PAT u on u.PAT_ID = k.PAT_ID;
You can then alter your schema back to XYZ and perform the same select statement and get different results based on the contents of the tables as owned by XYZ:
ALTER SESSION SET CURRENT_SCHEMA=XYZ;
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from DRUG_KIT k
left join DR_SHIP s on s.ID = k.SHIPMENT_ID
left join USR_PAT u on u.PAT_ID = k.PAT_ID;
To make the DR_SHIP table owned by PDQ accessable from the XYZ schema, a private synonym to PDQ.DR_SIP can be created in place of the table onwed by XYZ:
CREATE OR REPLACE SYNONYM DR_SHIP FOR PDQ.DR_SHIP;
On the other hand if you want the PDQ version of the DRUG_KIT table accessible from all SCHEMAs in the DB, you can create a public synonym:
CREATE OR REPLACE PUBLIC SYNONYM DRUG_KIT FOR PDQ.DRUG_KIT;
If none of the above meet your needs, you can use SQL*Plus style substitution variables:
ACCEPT user;
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from &user..DRUG_KIT k
left join &user..DR_SHIP s on s.ID = k.SHIPMENT_ID
left join &user..USR_PAT u on u.PAT_ID = k.PAT_ID;
But you need to understand that these won't necessarily work outside of SQL*Plus, SQL Developer, SQLcl, and possibly a couple of other SQL development environments.
At the database level, you probably want public synonyms. https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7001.htm
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