Update field values of table with field values that is in query - sql

UPDATE Tbl, Qry SET Tbl.Submit_Date = [Qry]![FirstOfTIMESTAMP]
WHERE (((Tbl.Info_ID)=[Qry]![INFO_ID]));
I want to update Tbl.Submit_Date with values from [Qry]![FirstOfTIMESTAMP] where both of their info_id are equal.
I get an error saying Operation must have an updateable query. I am using MSAccess. Any help is greatly appreciated.

As you learned, in MS Access queries must be updateable in order to be used in UPDATE queries. As a workaround consider converting your SQL aggregation to domain aggregation with DMin() assuming FirstOfTIMESTAMP is the Min() aggregation from a source table
UPDATE Tbl
SET Tbl.Submit_Date = DMin("TIMESTAMP","SrcTable","Info_ID=" & Tbl.[INFO_ID])

Related

Operation must use an updatable query

I am trying to update a column in one table to set its value to the count of records in another table. This produces the error:
Operation must use an updateable query.
Query:
UPDATE Tracking SET BatchCount = (Select Count(*) from Batch)
WHERE ReportingDate=Date();
It seems Access does not like the Select Count(*) from Batch sub-query. If I replace it with a literal value, it works fine.
Any suggestions to resolve this issue is much appreciated.
Unfortunately this is an inherent restriction of the JET database engine used by MS Access: no part of an update query may use aggregation, else the resulting recordset is not updateable.
There are a couple of workarounds:
You can use a domain aggregate function, such as DCount:
update tracking set batchcount = dcount("*", "Batch")
where reportingdate = date();
Alternatively, you can use a temporary table to store the result of the count, and then update the records using the value held in the table, e.g.
select count(*) as cnt into temptable from batch
update tracking, temptable set tracking.batchcount = temptable.cnt
where tracking.reportingdate = date();

Update from aggregate in same table if aggregate value wrong - SQL Server/Oracle/Firebird

I have a table with grouped tasks:
tt_plan_task_id is the id
records with tt_plantype=1 represent 'groups'
tasks in/under a group have a tt_group_id pointing to the tt_plan_task_id
there are tasks that don't belong to a group (tt_group_id is null)
groups nest multiple levels
I need to fix (update) the tt_fromdate field values for the group records if they do not match the min(tt_fromdate) from the underlying tasks (they always have a value).
To fix them all I could do
update tt_plan_task g
set tt_fromdate=
(select min(t.tt_fromdate) from tt_plan_task t
where (t.tt_group_id=g.tt_plan_task_id))
where (g.tt_plantype=1)
This statement avoids the UPDATE FROM syntax that I see in many (SQL server) answers - Firebird does not support that.
There are 2 complications
I want to do the update only if g.tt_fromdate <> min(t.tt_fromdate), so I would have to add a reference to min(tt_fromdate) to the outer where.
I tried using an alias for the aggregate and referencing that but that got me nowhere (syntax errors)
SQL Server does not like the table alias in the update, but solutions like these use the UPDATE FROM syntax again ;-( How do I work around that then?
How do I tie 1. and 2. into my update statement so that it works?
As noted in the title, this needs to execute in SQL Server, Oracle, and Firebird
Note: Since groups can contain groups, the update should ideally be executed 'from the bottom up', i.e. deepest groups first.
But since this is just a rough correction for a corrupt database, doing one 'lineair' pass over all groups is good enough.
To get around SQL Server's non-standard way for update table aliases, simply don't use any.
As to using the aggregate result in both the SET clause and the WHERE clause, I suppose the only way all DBMS work with, is to write the aggregation query twice.
update tt_plan_task
set tt_fromdate =
(
select min(t.tt_fromdate)
from tt_plan_task t
where t.tt_group_id = tt_plan_task.tt_plan_task_id
)
where (tt_plantype=1)
and
(
tt_fromdate <>
(
select min(t.tt_fromdate)
from tt_plan_task t
where t.tt_group_id = tt_plan_task.tt_plan_task_id
)
);

Set a column equal to a value in Oracle SELECT

I want to Select a column and set it equal to 0. In SQL I just do this: SELECT Dealer_Fee = 0.
Do I have to use an update? When I try the same thing in Oracle I get "FROM keyword not found where expected."
I am not sure what do you want to do and in which SQL dialect the construction you mentioned works. If you just want to retrieve some value and place it in a named column in Oracle you have to use DUAL table. Try this:
SELECT 0 AS dealer_fee FROM dual;
On the other hand, if you ment T-SQL and placing a value into variable you need to use PL/SQL SELECT INTO clause, like that:
SELECT 0 INTO dealer_fee from dual;
If not try to explain in more detail what are you trying to achieve.
1) In Oracle you must specify FROM clause within SELECT, is not like MS sqlserver where you can omit a FROM clause.
2) If you want to update one specific value, you must use UPDATE clause instead SELECT.
hth
You should use the Update statement. Please visit this oracle reference which explains the Update statement in detail: Update Statement
Take a look at the reference for UPDATE in Oracle:
UPDATE <table_name>
SET <column_name> = <value>

How to write a update SQL using a select SQL

I want to write a update SQL statement, but one conidtion of this statement is the result from a select SQL statement, and I also want to return the result of the select SQL statement.
Like this: update ... set ... where id = (select id from ...)
I want to return the value of id back.
Does anybody know how should I do this?
Thanks in advance!
I don't believe that's possible in one statement. Update then query (select) the new value, or query the value first, and then submit an update.
Alternative would be a stored procedure on the database, which executes the multiple queries and returns the result for you.
This is not possible in all Java database frameworks that I know. Probably you need to separate your query and update in Java.
I don't see any problem in using a subselect in a WHERE clause of an update statement.
For the second request, getting back the value of id, I know this is possible in DB2, and maybe others implement that syntax too:
SELECT id FROM FINAL TABLE (
update ... set ... where id = (select id from ...)
)
This works also for INSERT and DELETE statements. (See the documentation.)
Update statements won't return the updated datasets. The select in that case would be a subselect that isn't directly accessible.
You'd thus have to use at least two queries:
select the ids you want
call the update query passing the previously selected ids as a parameter

vb.net access query for updating

is d query rite ?
q = "update FIRSTBBA WHERE FBBANAME<>"" ORDER BY FBBANAME"
Need to update a table in ascending order
This query is not complete. You can use inner SQL statement based on your requirement and update the table based on that.
You're not assigning a value to FIRSTBBA.
Also, not sure what database you're using but I don't think SQL server allows an order by clause in an update statement. you would have to refer to a post like this one: How to update and order by using ms-sql