Handling cannot insert Null into in Oracle - sql

I have an insert statement where in i am inserting the data from table_abc to table_job. Now there is mandatory column in effec_start_date in table_job,
but this effec_start_date is not mandatory in table_Abc. Hence there may be null values too in effec_start_date of table_abc.
While inserting from table_abc to table_job it is going into exception
stating
-1400 - ORA-01400: cannot insert NULL into ("HR"."table_job"."EFF_START_DATE")
and obviously when querying table_job it is not returning any value
Is there a possibility to insert the not null values for effective_Start_date in table_job and for only null values it goes into exception
insert into table_job
(job_code,
eff_start_date,
eff_end_date,
config_id
)
select * from table_Abc;

you need to filter your data in the select statement
insert into table_job
(job_code,
eff_start_date,
eff_end_date,
config_id
)
select * from table_Abc
where eff_start_date is not null;

Related

INSERT in CASE expression from subselect PostgreSQL

I am trying to create a query like this
WITH insert1 as (),
...
subselect1 as (SELECT
(CASE
WHEN %s is NOT NULL THEN
(INSERT INTO Duration (duration)
VALUES (ROW (%s, %s)) RETURNING id as id_duration)
ELSE
(INSERT INTO Distance (length)
VALUES (ROW (%s, %s)) RETURNING id as id_distance)
END)),
...
INSERT INTO FinalTable...
I'm having trouble with the syntax, I know. Do you accomplish this by with Insert into?
My plan is:
By one WITH statement make several insertions with returning values and finally insert to the FinalTable. Having only INSERT and RETURNING values it works great - I have to refer them in FinalTable e.g. (SELECT id_point from insert3).
But this case - I would like to return value from insert, wrapped in CASE (%s means parametrized query, variables passed from python). So in case first %s is NOT NULL, I have to insert to table Duration, else I have to insert to table Distance.
When inserting in FinalTable, I have references to these tables (columns idDistance, idDuration) - so I would like to write smth like (..., (SELECT id_duration from subselect1), (SELECT id_distance from subselect1)...)
What's wrong with my syntax?
It's very unclear to me what you are trying to achieve, but maybe you are looking for something like this:
WITH insert1 as (
...
),
insert_duration (id_duration) as (
insert into duration (duration, ....)
select ....
from (
values (..),(..)
) as t(...)
where $1 IS NOT NULL --<< first branch of your CASE expression
returning id
),
insert_distance (id_distance) as (
insert into distance (length, ...)
select ....
from (
values (..),(..)
) as t(...)
where $1 IS NULL --<< ELSE branch of your CASE expression
returning id
)
INSERT INTO final_table
...

Insert using IF/ELSE statements

I am sorry if my question is not clear or my query is not sufficient to help. I have a procedure that has multiple if/else statement. My goal is to insert one row if that if statements meets the criteria else go further. Something like this:
create or replace procedure abc.xyz
( i_name varchar2
,number number,
sections varchar2)
...
max_date date;
min_date date;
...
if(sum=0)
insert into abc_table
(id,name,number,sections,description,date,amount,price,source,latest_date)
select user_seq.nextval,name,number,max_date,amount,0
,'xyz',trunc(sysdate))
from abc_table x
where x.name=i_name
and x.number=i_name
and x.section=i_section;
elseif (sum>0)
insert into abc_table
(id,name,number,sections,description,date,amount,price,source,latest_date)
select user_seq.nextval,name,number,max_date,amount,0
,'xyz',trunc(sysdate))
from abc_table x
where x.name=i_name
and x.number=i_name;
and x.section=i_section;
when i run my procedure, to insert the calculated value , the values are correct but so many rows were inserted. How can I prevent from multiple insert and make only one row insert ?
You are doing it wrong.
According to the Oracle documentation, The syntax for the Oracle INSERT statement when inserting a single record using the VALUES keyword is:
INSERT INTO table
(column1, column2, ... column_n )
VALUES
(expression1, expression2, ... expression_n );
But the syntax for the Oracle INSERT statement when inserting multiple records using a SELECT statement is:
INSERT INTO table
(column1, column2, ... column_n )
SELECT expression1, expression2, ... expression_n
FROM source_table
[WHERE conditions];
reference: https://www.techonthenet.com/oracle/insert.php
from the link i shared:
If you don't want to insert duplicate:
INSERT INTO clients
(client_id, client_name, client_type)
SELECT 10345, 'IBM', 'advertising'
FROM dual
WHERE NOT EXISTS (SELECT *
FROM clients
WHERE clients.client_id = 10345);

Cannot insert the value NULL into column CreationTime

Runtime Exception :
Cannot insert the value NULL into column 'CreationTime', table
'MyTables'; column does not allow nulls. INSERT
fails.
Code:
INSERT INTO [MyTables] (LegacyId, CreationTime)
SELECT DISTINCT
a.[IPLID], a.[inputdate]
FROM
[Legacy].[dbo].[MyTables2] AS a
Can you tell me how to insert custom date like 01/01/2000 when a.[inputdate] is Null ?
just wrap in an ISNULL:
INSERT INTO [MyTables] (LegacyId,CreationTime)
SELECT DISTINCT a.[IPLID],ISNULL(a.[inputdate], '01/01/2000')
FROM [Legacy].[dbo].[MyTables2] as a
Use the ISNULL function.
INSERT INTO [MyTables] (LegacyId,CreationTime)
SELECT DISTINCT a.[IPLID],ISNULL(a.[inputdate], '01/01/2000') FROM [Legacy].[dbo].[MyTables2] as a

why inserting a null value to not null column do not terminate sql script but conversion failure do

DECLARE #tempdb AS TABLE (col INT NOT NULL)
INSERT INTO #tempdb ( col )VALUES (1)
--part1:
--Cannot insert the value NULL into column 'col', table '#tempdb'; column does not allow nulls. INSERT fails.
INSERT INTO #tempdb ( col )VALUES (NULL)
--the whole script didn't abort
SELECT * FROM #tempdb -- this line is executed
--part2:
--Conversion failed when converting the varchar value 'asdsd' to data type int.
INSERT INTO #tempdb ( col )VALUES ('asdsd')
--script terminated here
SELECT * FROM #tempdb --didn't execute
the two cases above both got error, but why case 2 terminated sql script and case 1 didn't?

Get the wrong line ID in ms sql

I have an INSERT statment wich inserts large amount of data into tableA from tableB.
Here is a very simple code example:
INSERT [dbo].[tableA]
SELECT field1 [field_1]
FROM [dbo].[tableB]
WHERE [codeID] IN (SELECT [codeID] FROM #tempTable WHERE RecordMarker = 1)
There is a temporary table wich holds codeIDs (at least 1 or more) needed to insert to tableA.
But there would be incorrent data in tableB what cannot be inserted into tableA. For example an numberic(30,2) field cannot map to numeric(13,2). In this case I get an excetpion and the statement has been terminated.
How can I get the CodeID or the wrong line number in tableB if I get an error? Now I have just the error message but no line number.
For example:
Msg 8115, Level 16, State 8, Line 1
Arithmetic overflow error converting numeric to data type numeric.
The statement has been terminated.
EDIT: There are more than one field in the table with different field types. So the numeric type is just an example.
Please try the following:
INSERT [dbo].[tableA]
SELECT field1 [field_1]
FROM [dbo].[tableB]
WHERE [codeID] IN (SELECT [codeID] FROM #tempTable WHERE RecordMarker = 1)
AND [codeID] <= 9999999999999.99;
INSERT ErrorLog
SELECT *
FROM [dbo].[tableB]
WHERE [codeID] > 9999999999999.99;
If you know the type of the destination field you're having the issue with, in this case a numeric of (13,2) precision, you can run a SELECT with a TRY_CONVERT on the potential problem field against your temp table and filter for NULL results. You could add a WHERE clause to your insert statement if you wanted to ensure that it would run successfully and not try to insert those "bad" rows.
CREATE TABLE #t (x NUMERIC(30,2),field2 varchar(10))
INSERT INTO #t
SELECT 123456789.23,'x'
UNION
SELECT 12345678901212343.23,'y'
UNION
SELECT 12345678923523523235.23,'z'
UNION
SELECT 42.0, 'a'
SELECT *, TRY_CONVERT(NUMERIC(13,2),x,1) [Converted to numeric(13,2)] FROM #t
Reference: http://msdn.microsoft.com/en-us/library/hh230993.aspx