ORACLE : Update multiple columns error : Can not update string to NULL - oracle

I have a simple update stament as:
UPDATE table_1 t1 SET (t1.a, t1.b, t1.c) =
(SELECT t2.x, t2.y, t3.z
FROM table_2 t2, table_3 t3, table_4 t4
WHERE
1 = 1
AND t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
) WHERE EXISTS
(SELECT 1 FROM t2
WHERE
1 = 1
AND t2.name = t1.name
)
Inner SELECT query gives me results. But when I execute whole UPDATE query, it fails with error:
ORA-01407 "cannot update (%s) to NULL"
It also says that update on t1.a to NULL not possible. Please let me know, if I have made any mistake in this update query.
Please note that I can not use MERGE statement because there is a possibility that t2.name = t1.name condition may give me more than one entry from table t2. And in this case merge statement may fail with error can not get stable set of rows. If there is any other better appraoch let me know.
Update: t1.a has NOT NULL constraints. And my inner query returns results which do not have t2.x as NULL.

Problem can be that your SQL in SET part is more selective than SQL in EXITS. If so, change your exists to
... EXISTS ( SELECT 1 from
FROM table_2 t2, table_3 t3, table_4 t4
WHERE
t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
Also all your x,y, or z columns should have not null value if you a,b,c are defined as not null (all x should be not null if t1.a is not null etc)

I found the reason why I was getting that error. With condition t2.name = t1.name I was getting two rows in table table_1. For first row I had value t2.x from inner select query. For second row I did not. Thus I was getting the error. I modified my query as below:
UPDATE table_1 t1 SET (t1.a, t1.b, t1.c) =
(SELECT t2.x, t2.y, t3.z
FROM table_2 t2, table_3 t3, table_4 t4
WHERE
1 = 1
AND t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
) WHERE t1.id, t1.name IN
(SELECT t1.id, t1.name FROM table_2 t2, table_3 t3, table_4 t4
WHERE
1 = 1
AND t2.id = t3.id
AND t2.id1 = t4.id1
AND t2.name = t1.name
)
Hope it will help others.

Related

Oracle Hierarchical queries: Translate START WITH ... CONNECT BY PRIOR into 'Recursive Subquery Factoring'

How would the following START WITH / CONNECT BY hierarchical query look like when translated into a RECURSIVE SUBQUERY FACTORING hierarchical query with WITH clause:
SELECT t1.id
FROM table1 t1, table2 t2
WHERE t1.version_id = t2.id
AND t1.baseline_date = TRIM (TO_DATE ('2015-05-26', 'yyyy-mm-dd'))
AND t2.entry_date = t1.baseline_date
START WITH t1.id IN (SELECT id
FROM table1
WHERE parent_id = 101015)
CONNECT BY PRIOR t1.id = t1.parent_id
ORDER SIBLINGS BY t1.child_index;
I think you want:
WITH rsqfc (id, child_index, baseline_date) AS (
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM table1 t1
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
WHERE t1.parent_id = 101015
UNION ALL
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM rsqfc r
INNER JOIN table1 t1
ON (r.id = t1.parent_id)
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
)
SEARCH DEPTH FIRST BY child_index SET order_id
SELECT id
FROM rsqfc
WHERE baseline_date = DATE '2015-05-26';
However, without sample data it is difficult to be sure.

Comparing two columns one of which is null

I have three tables as follow
TABLE1
ID VALUE
1 NULL
TABLE2
ID VALUE
1 1
TABLE3
ID VALUE
1 10
I'm trying to do the following:
Compare the two fields (ID) in tables 1 & 2 if one is null return null then return value from TABLE3.
It seems you are looking for something like this:
select table3.id, table3.value
from table1 join table2 on table1.id = table2.id
join table3 on table1.id = table3.id
where table1.value is null or table2.value is null
If this isn't what you had in mind, you need to give more details about your requirement.
Try this query:
SELECT t1.id,
(CASE WHEN t1.value IS NULL OR t2.value IS NULL
THEN t3.value
END) VALUE
FROM table_1 t1
JOIN table_2 t2
ON t1.id = t2.id
JOIN table_3 t3
ON t1.id = t3.id

Return non-null value from two tables in Oracle

I have two tables, T1 and T2 with same set of columns. I need to issue a query which will return me value of columns from either table whichever is not null. If both columns are null return null as the value of that column.
The columns are c1,c2,c3,cond1.
I issued the following query. The problem is that if one subquery fails the whole query fails. Somebody please help me. Probably there is another simple way.
SELECT NVL(T1.c1, T2.c1) c1,NVL(T1.c2, T2.c2) c2,NVL(T1.c3, T2.c3) c3
FROM (SELECT c1,c2,c3
FROM T1
WHERE cond1 = 'T10') T1
,(SELECT c1,c2,c3
FROM T2
WHERE cond1 = 'T200') T2 ;
You need something like this:
SELECT NVL((SELECT T1.c1
FROM T1
WHERE T1.c2 = 'T10'),
(SELECT T2.c1
FROM T2
WHERE T2.c2 = 'T200')) AS c1
FROM dual
Or you may prefer a full outer join:
SELECT NVL(T1.c1, T2.c1) AS c1
FROM T1 FULL OUTER JOIN T2 ON 1=1
WHERE T1.c2 = 'T10'
AND T2.c2 = 'T200'
Your result is logical. If the first table is null no combination of values will exist in the natural join.
EDIT. After some new requirements we can use a hack to get the row. Lets get all three possibilities, T1, T2 or all nulls and select the first one:
SELECT *
FROM ( (SELECT T1.*
FROM T1
WHERE T1.c2 = 'T10')
UNION ALL
(SELECT T2.*
FROM T2
WHERE T2.c2 = 'T200')
UNION ALL
(SELECT T2.*
FROM dual
LEFT JOIN T1 ON 1 = 0 ) )
WHERE ROWNUM = 1

Want to change SQL query to LINQ

I want to change the below simple SQL query into LINQ , how do I change it ?
select * from table1 where isPaid = 'true' and Id in (select Id from table2 where EmployeeId = 12)
similar to this ?
from pa in db.PaymentAdvices
where pa.IsPaid == true
orderby pa.PaidDate descending
select pa;
Here code linq to sql:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12
select t1
Hope usefull!
if the field isPaid has datatype is Boolean:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid == true
select t1
if the field isPaid has datatype is String:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid.Equals("true")
select t1

2x COUNT in HAVING clause

I have following problem:
I want to count data in one table, count data in second table and compare countings in having clause, and display only that rows which have the same countings
Something like that:
SELECT bla
FROM T1 t1 JOIN T2 t2
ON t1.id = t2.id
HAVING COUNT(counted data from table1) = COUNT(counted data from table2)
Do you have any idea?
Cheers
Standard SQL:
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) AS t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) AS t2
ON t1.id = t2.id AND t1.counter = t2.counter
Oracle SQL (because Oracle doesn't like AS before table aliases):
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) t2
ON t1.id = t2.id AND t1.counter = t2.counter
You just have to decide where bla comes from; I nominated t1. I'm assuming that for any given value of t1.id, there is a single value of t1.bla. If there isn't, then you need to explain much more clearly what you're counting and where the various columns are, and what the keys of the tables are.
Update: Apologies for not noticing the Oracle tag and giving invalid Oracle syntax.
WITH jezyki as
(SELECT pseudo_wampira, COUNT(*) AS counter
FROM Jezyki_obce_w
GROUP BY pseudo_wampira
)
,sprawnosc as
(SELECT pseudo_wampira, sprawnosc, COUNT(*) AS counter
FROM Sprawnosci_w
GROUP BY pseudo_wampira, sprawnosc
)
SELECT jezyki.pseudo_wampira, sprawnosc.counter
FROM jezyki,sprawnosc
WHERE jezyki.pseudo_wampira = sprawnosc.pseudo_wampira
AND jezyki.counter = sprawnosc.counter

Resources