I have a sql statement that returns records that I need and I am using several WITH clauses. I am trying to update one column within the statement.
WITH <name_a> AS (SELECT...),
<name_b> AS (SELECT...using 'name_a'),
<name_c> AS (SELECT...using 'name_b')
SELECT * from <name_c>
How would I write the statement if I just want to update a column in the final <name_c> output? Ex.
UPDATE name_c SET fav_color = 'blue'
for all the records.
I tried just replacing the select with the update statement but I get 'Missing SELECT keyword'.
I found a similar question but it looks like this question focused on updating the value to what was returned from the select statement. Using "WITH" and "UPDATE" statements in the same SQL query
Related
I'm trying to insert values into a table inside a CASE statement, but I'm getting the error "missing expression" in my INSERT INTO.
What is the correct way to use INSERT INTO within CASE? You can find the code below. I am using this SELECT statement in a procedure, and I'm putting the result of this into a cursor.
SELECT
COALESCE(a.file_type,b.file_type) AS file_type,a.input AS a_input,b.input AS b_input,a.output AS a_output,b.output AS b_output,
CASE WHEN a.input = b.input THEN
input_num+1
WHEN a.input <> b.input THEN
INSERT INTO diff_values(file_type,a_input,b_input,report_date)
SELECT file_type,a.input,b.input FROM test1 a, tset2 b WHERE
a.file_type=b.file_type
WHEN a.output = b.output THEN
out_num+1
END CASE
FROM
test1 a, tset2 b
WHERE a.file_type=b.file_type
AND a.report_date=b.report_date;
You cannot embed a DML statement (e.g. INSERT) within a SQL query.
You cannot embed a CASE statement (which is PL/SQL) within a SQL query. You can embed a CASE expression within a SQL query, however, but the expression can only evaluate a result, it cannot include PL/SQL (see #1 above)*.
The outcome you seem to want to achieve is to query some data from two tables (test1 and tset2), evaluate an expression, and insert the result back into the same tables as new records. Since your final outcome is an INSERT, you would start with that and then create a query that gathers the data needed for it, e.g.:
INSERT INTO diff_values(file_type,a_input,b_input,report_date)
SELECT a.file_type,
a.input,
b.input,
a.report_date
FROM test1 a, tset2 b
WHERE a.file_type=b.file_type
AND a.report_date=b.report_date
AND a.input <> b.input;
Note that I've removed the COALESCE because the WHERE clause guarantees both a.file_type and b.file_type are identical anyway, so you can just refer to one of those columns.
Now, your original statement included the following lines as well:
WHEN a.input = b.input THEN
input_num+1
...
WHEN a.output = b.output THEN
out_num+1
I don't know what these are supposed to do, but I suppose you could write additional queries to detect these conditions and output the results.
technically, in Oracle 12.1 you can include procedural code in a WITH clause within SQL https://oracle-base.com/articles/12c/with-clause-enhancements-12cr1
I am using APEX collections to store some values and pass them between pages in Oracle Application Express 4.2.3.
I would like to then perform an update statement on a table called "project" with the values from the collection.
My code so far is as follows:
update project
SET name=c.c002,
description=c.c007,
start_date=c.c004,
timeframe=c.c005,
status=c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
and id = :p14_id;
where :p14_id is the value of a page item.
However, I am getting the following error:
ORA-00933: SQL command not properly ended
Anyone have any idea on how to approach this?
Thanks!
The UPDATE syntax you are using is not valid in Oracle; it does not allow you to use FROM in the way you are attempting.
The simplest way to do this in Oracle would with a subquery:
update project
set (name, description, start_date, timeframe, status) =
(select c.c002, c.c007, c.c004, c.c005, c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
)
WHERE
id = :p14_id
;
Note that if the subquery returns no rows, the columns in the target table will be updated to NULL; this could be avoided by adding a similar EXISTS condition in the predicate for the update. It could also be avoided by using a MERGE statement instead of an UPDATE.
If the subquery returns multiple rows, the statement will throw an error. It looks like tthat should not be the case here.
Can anyone tell me what is wrong with this update statement i want it to update the title of Dr Edders and then display Dr Edders after the statement has been run.
UPDATE Staff SET Title = 'Dr' WHERE StaffLName = 'Edders';
SELECT Title, StaffLName FROM Staff WHERE StaffLName = 'Edders';
For oracle you can use RETURNING INTO clause to return what you are updating, deleting or even inserting
You can find more information here DML RETURNING INTO Clause
For SQL Server you can use OUTPUT clause
Find the MSDN page here OUTPUT Clause (Transact-SQL)
Apart from which database you are using, you can also put your UPDATE and SELECT statement into a procedure and execurte the stored procedure.
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>
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