change the value of a query when inserting - oracle

tab1
column1 =4
column2 = (empty column )
column3 = also empty
what I want is when I insert data into column2
column2 = test
I want column 2 to have the data of column1 , I want to have such result
column 2= 4test
I know I can achieve that with a trigger or procedure , I am asking you if there is another way when I insert it

Try
update tab1 set column2 = column1 || 'test' where condition;

Related

Oracle CASE WHEN - ORA-00936: missing expression

I am new to oracle and below is my SQL.
SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES'
AND COLUMN2 IN (
CASE WHEN EXISTS(SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%NO%')
THEN
SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%'
ELSE
SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%' END)
it is giving ORA-00936: missing expression at then statement. What am I doing wrong?
The subqueries after THEN and ELSE must be enclosed inside parentheses:
SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES'
AND COLUMN2 IN (
CASE
WHEN EXISTS (SELECT * FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%NO%')
THEN (SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%')
ELSE (SELECT COLUMN2 FROM TABLE1 WHERE COLUMN1 = 'YES' AND COLUMN2 NOT LIKE '%YES%')
END
)
This will work only if these subqueries don't return more than 1 row.
Also, both subqueries are the same. Is this a typo?
And IN can be changed to = since CASE returns only 1 value.

Check for != values when column contains records with NULLs

I have a number of tables that mix 'real' values with nulls in columns. From exerience, issuing a SELECT against these that looks like:
SELECT column1, column2, column3 FROM mytable WHERE column1 != 'a value';
...doesn't return the records I expect. In the current table I am working on, this returns an empty recordset, even though I know I have records in the table with NULLs in column1, and other records in the table that have the value I am "!="ing in column1. I am expecting, in this case, to see the records with NULLs in column1 (and, of course, anything else if there were other not 'a value' values in column1.
Experimenting with NVL in the WHERE clause doesn't seem to give me anything different:
SELECT column1, column2, column3 FROM mytable WHERE NVL(column1, '') != 'a value';
...is also returning an empty recordset.
Using 'IS NULL' will technically give me the correct recordset in my current example, but of course if any records change to something like 'another value' in column1, then IS NULL will exclude those.
NULL can't be compared in the same way that other values can be. You must use IS NULL. If you want to include NULL values, add an OR to the WHERE clause:
SELECT column1, column2, column3 FROM mytable WHERE column1 != 'a value' OR column1 IS NULL
The query doesn't work because SQL handles equality checks (!=) different from checking if null (IS NULL).
What you could do here is something like:
SELECT column1, column2, column3
FROM mytable
WHERE column1 != 'a value' OR column1 is null;
See Not equal <> != operator on NULL.
What you were trying was correct. You just need change it a little. see below-
SELECT column1, column2, column3 FROM mytable WHERE NVL(column1, '0') != 'a value';
Instead of empty string, pass any character in NVL's second argument.
"Experimenting with NVL in the WHERE clause doesn't seem to give me anything different"
That's true:
SQL> select * from mytable;
COLUMN1 COLUMN2 COLUMN3
-------------------- ---------- ---------
a value 1 25-JUL-17
not a value 2 25-JUL-17
whatever 3 25-JUL-17
4 26-JUL-17
SQL> SELECT column1, column2, column3 FROM mytable WHERE NVL(column1, '') != 'a value';
COLUMN1 COLUMN2 COLUMN3
-------------------- ---------- ---------
not a value 2 25-JUL-17
whatever 3 25-JUL-17
SQL>
This is because your experiment didn't go far enough. For historical reasons Oracle treats an empty string as null so your nvl() statement effectively just subs one null for another. But if you had used a proper value in your call you would have got the result you wanted:
SQL> SELECT column1, column2, column3 FROM mytable WHERE NVL(column1, 'meh') != 'a value';
COLUMN1 COLUMN2 COLUMN3
-------------------- ---------- ---------
not a value 2 25-JUL-17
whatever 3 25-JUL-17
4 26-JUL-17
SQL>
The alternative approach is to explicitly test for NULL and test for the excluding value...
SQL> SELECT column1, column2, column3 FROM mytable
2 where column1 is null or column1 != 'a value';
COLUMN1 COLUMN2 COLUMN3
-------------------- ---------- ---------
not a value 2 25-JUL-17
whatever 3 25-JUL-17
4 26-JUL-17
SQL>
The second approach is probably more orthodox.
1) Undocumented Oracle function SYS_OP_MAP_NONNULL. (It exists from oracle10)
with abc as ( select 'a value' as col1 from dual
union all
select '' as col1 from dual)
select * from abc
where SYS_OP_MAP_NONNULL(col1) != SYS_OP_MAP_NONNULL('a value')
;
2) LNNVL - Check table in the documentation for clarification.
with abc as ( select 'a value' as col1 from dual
union all
select '' as col1 from dual)
select * from abc
where lnnvl( col1 = 'a value');
;

Oracle Merge: Update single record in base table using multiple records from source table

I have a merge query as follows:
merge into table1
using table2
on (table1.column1 = table2.column1)
when mateched then
update
set column2 = table2.column2;
Now it is giving me error like :unable to get a stable set of rows in the source tables
Now the issue is the source table, table2, is having multiple records for same column1 value and table1 is having only one record per column1 value.And I need all the table2 to update table1. can you pls help here?
" the issue is the source tabletable2, is having multiple records for same column1 value "
You need to ensure your sub-query returns only one row per value of column1. Only you know what exact business rule you need to apply, but it could be something like this:
merge into table1
using ( select column1, max(column2) as column2
from table2
group by column1 ) t2
on (table1.column1 = t2.column1)
when matched then
update
set column2 = t2.column2;
"I need all records from table2 to update table1. The reason is that I have a trigger on table1 which captures all transactions(inserts/updates) and loads to another history table. "
You can't use MERGE. In fact you're probably going to have to go procedural:
begin
for t2rec in (select column1, column2
from table2
order by column1, column2 )
loop
update table1
set column2 = t2rec.column2
where column1 = t2rec.column1;
end loop;
end;
You'll need to order the loop statement to ensure the end state of table1 is right.
Alternatively you could disable the journaling trigger and do this:
insert into table1_journal
select table1.column1
, table2.column2
from table1
join table2
on table1.column1 = table2.column1
/
Then do the MERGE or a single row update.

I want to return data from duplicate rows SQL Query

I want to return data from duplicative rows
SELECT column1, column2 FROM table1
COLUMN1 COLUMN2
------- -------
CA 1
CB 2
CB 3
CC 4
CD 5
CE 6
CE 7
CE 8
CF 9
I want to return rows for 'CB' and CE. Here CB and CE has more than 1 row.
I'd code this as follows:
SELECT column1, column2 FROM table1 where column1 in ("CB", "CE")
Try this out - this query first finds out those items in column1 who appear multiple time and then extract their information.
select * from table1
where column1 in (
select column1
from table1
group by column1
having count(*) > 1
)
If you are only interested in knowing the values in column1, you could just run:
select column1
from table1
group by column1
having count(*) > 1
You Can try out this code. Basically the Query is in MySQL but you can use the same logic in Oracle Database. Here the inner subquery will find out the columns which is grouped by column1 and will return a column having a count greater than 1. The Outer query will display the rows of the column fetched by the inner query.Here I have created a table with the table name as name
SQL fiddle Added for your reference SQLCODE
select * from name where column1 in(select column1 from name
group by column1
having count(*)>1);

Updating column1 in table1 only if matched column3 in table2

I want to update column1 in table1 only if the column2 in table1 match the column3 in table2.
I am trying to do using this query but I got an error that says that I am missing equal sign.
Can anyone can help?
update schema1.table1
set schema1.table1.column1
where schema1.table1.column2 = table2.column1
Your error says it all. You are not assigning any value to the column. Try to set the value using the equal = sign
You may try this:
update schema1.table1
set schema1.table1.column1 = //The value which you want to store
where schema1.table1.column2 = table2.column1
Try this query:
update schema1.table1 t1
set t1.column1 = (select t2.columnX from table2 t2
where t1.column2 = t2.column1)
where t1.column2 in (select column1 from table2)
As in the error message, you have missed an = and not assigning any value for schema1.table1.column1 in the query.
Try like this:
UPDATE schema1.table1
SET schema1.table1.column1 = <your_value>
WHERE schema1.table1.column2 = table2.column1;

Resources