Column name checking null still giving null error - sql

I want to insert values in a table. So I have written the below code for that.
create or replace PROCEDURE NEIQC_DATA_DUMP_MST AS
BEGIN
execute immediate 'truncate table TBL_NEIQC_WF_SITE_MST_NEW';
INSERT INTO TBL_NEIQC_WF_SITE_MST_NEW
(
OBJECTID,
SAP_ID,
NETWORK_ENTITY_ID ,
SITE_NAME ,
SITE_ADDRESS ,
MAINTENANCEZONE_CODE ,
INVENTORY_TYPE ,
TYPE_NAME ,
SITE_STATUS_CODE ,
NE_MODIFIED_DATE ,
NE_MODIFIED_BY ,
--SERVICE_CODE,
CREATED_DATE ,
CREATED_BY ,
STRUCTURE_NAME ,
RJ_CITY_CODE ,
RJ_R4G_STATE_CODE ,
RJ_DISTRICT_CODE ,
RJ_TALUK_CODE ,
RJ_JC_CODE ,
RJ_JIOPOINT_SAPCODE ,
RJ_COMPANY_CODE_1 ,
RJ_COMPANY_CODE_2 ,
PLACEMENT_DATE,
SITE_TYPE
)
SELECT
OBJECTID ,
RJ_SAPID ,
RJ_NETWORK_ENTITY_ID ,
RJ_SITE_NAME ,
RJ_SITE_ADDRESS ,
RJ_MAINTENANCE_ZONE_CODE ,
CASE when RJ_SAPID LIKE '%ENB%' THEN 'SITE' ELSE 'OTHERS' end as INVENTORY_TYPE,
TYPE_NAME ,
4,
RJ_LAST_MODIFIED_DATE,
RJ_LAST_MODIFIED_BY ,
--APP_FIBERINV.FUNC_SC(RJ_SAPID),
SYSDATE,
'SCHEDULER',
STRUCTURE_NAME ,
RJ_CITY_CODE ,
RJ_R4G_STATE_CODE ,
RJ_DISTRICT_CODE ,
RJ_TALUK_CODE ,
RJ_JC_CODE ,
RJ_JIOPOINT_SAPCODE ,
RJ_COMPANY_CODE_1 ,
RJ_COMPANY_CODE_2 ,
PLACEMENT_DATE,
REGEXP_SUBSTR(TO_CHAR(RJ_SAPID),'[^-]+', 1, 4) AS SITE_TYPE
from APP_FTTX.ne_structures WHERE RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA' AND OBJECTID IS NOT NULL;
COMMIT;
END NEIQC_DATA_DUMP_MST;
But the issue is, even after checking the null condition for OBJECTID I am still getting error as
ORA-01400: cannot insert NULL into ("APP_FIBERINV"."TBL_NEIQC_WF_SITE_MST_NEW"."OBJECTID")
Please suggest what may be wrong here.

This is probably down to operator precedence; AND has higher precedence than OR. So your condition:
WHERE RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA' AND OBJECTID IS NOT NULL
is interpreted as
WHERE RJ_SAPID IS NOT NULL OR (RJ_SAPID <> 'NA' AND OBJECTID IS NOT NULL)
If you have a source row where OBJECTID is null then it will still pass than condition, and fail on insertion, if RJ_SAPID is anything except 'NA', including if that is null.
I believe you want:
WHERE (RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA') AND OBJECTID IS NOT NULL
so you need to include those parentheses in your statement.

i think your where condition should be improved. add '()' around your or condition
WHERE (RJ_SAPID IS NOT NULL OR RJ_SAPID <> 'NA') AND OBJECTID IS NOT NULL;

Related

Find if two columns exist in another table, matched as a pair

I've seen some other questions about comparing multiple columns, but I'm not sure they fit this exact need.
I'm trying to ensure an exact pair of columns in one table exists as the exact same pair of columns in another table. The goal is to check and mark a bit column as true false if it exists.
The last part of this script returns a 1, but I'm not sure if the logic ensures the exact pair is in the second table.
Is the logic in the last part of the script comparing both tables correct?
Sample Data:
CREATE TABLE #t1
(
courseid VARCHAR(10)
,courseNumber VARCHAR(10)
)
INSERT INTO #t1(
courseid
, courseNumber
)
VALUES
(3386341, 3387691)
CREATE TABLE #t2
(
courseid VARCHAR(10)
,courseNumber VARCHAR(10)
,CourseArea VARCHAR(10)
,CourseCert VARCHAR(10)
,OtherCourseNum VARCHAR(10)
)
INSERT INTO #t2(
courseid
, courseNumber
, CourseArea
, CourseCert
, OtherCourseNum
)
VALUES
(3386341 , 3387691 , 9671 , 9671 , 233321)
,(3386341 , 3387691 , 9671 , 9671 , 233321)
,(3386342 , 3387692 , 9672 , 9672 , 233322)
,(3386342 , 3387692 , 9672 , 9672 , 233322)
,(3386343 , 3387693 , 9673 , 9673 , 233323)
,(3386343 , 3387693 , 9673 , 9673 , 233323)
SELECT
CASE WHEN courseid IN (SELECT courseid FROM #t1) AND courseNumber IN (SELECT courseNumber FROM #t2) THEN 1 ELSE 0 END AS IsCourse
FROM #t1
I would always opt for an exists correlation here as it's faster and NULL-safe
select
case when exists (
select * from #t2 t2
where t2.courseid = t1.courseId and t2.courseNumber = t1.courseNumber
) then 1 else 0 end IsCourse
from #t1 t1;
No, your query doesn't correlate the id & number so your query will return a positive if the id exists anywhere in t2 and the number exists anywhere in t2, but doesn't confirm they exist on the same row. You want EXISTS e.g.
SELECT
CASE WHEN EXISTS (
SELECT 1
FROM #t2 t2
WHERE t2.courseid = t1.courseid
AND t2.courseNumber = t1.courseNumber
) THEN 1 ELSE 0 END AS IsCourse
FROM #t1 t1;

Select and insert on the same table

Can you tell me what is wrong with this query:
INSERT INTO properties 
(
f_gen_id(NULL)
, 'ASHAgroup18E'
, entity
, effective_dt
, property_key
, property_value
, created_by
, create_ts
, updated_by
, update_ts
) 
SELECT f_gen_id(NULL)
, 'ASHAgroup18E'
, entity
, effective_dt
, property_key
, property_value
, description
, created_by
, create_ts
, updated_by
, update_ts
-- 'UIL-Migration', CURRENT_TIMESTAMP, 'UIL-Migration', CURRENT_TIMESTAMP 
FROM properties
WHERE group_key = 'ASHAgroup18B';
In the INSERT part you should have column names, not a function
INSERT INTO properties (f_gen_id(NULL),...
replace this with the name of the primary key/id column
INSERT INTO properties (id,...
Update
I guess also the second column in the INSERT is incorrect since it contains a string. Again, replace it with a column name

Error occurs: "Incorrect syntax near ' , ' "

Insert Into Location
('Amman' , 'Sweleh' , 'Jordan') ,
('Zarqa' , 'Hussen' , 'Jordan') ,
('Jerash' , 'jenna' , 'Jordan') ,
('Ajloun' , 'shajra' , 'Jordan'),
('Irbid' , 'Hoson' , 'Jordan') ;
You have syntax error, you have missed VALUES keyword. If you want to omit the fieldname then try this:
INSERT INTO Location
VALUES
('Amman' , 'Sweleh' , 'Jordan') ,
('Zarqa' , 'Hussen' , 'Jordan') ,
('Jerash' , 'jenna' , 'Jordan') ,
('Ajloun' , 'shajra' , 'Jordan'),
('Irbid' , 'Hoson' , 'Jordan');
Or use this format :
INSERT INTO table(column1,column2...)
VALUES (value1,value2,...),
(value1,value2,...),
...
for more information read this mysql-insert-multiple-records
The correct syntax of an INSERT STATEMENT statement is:
insert into tableName (field1Name,...,FieldnName) values (value1,...,valuen)
Furthermore if you want to insert multipbe records in a singe query have a look at
this
Correct syntax :
insert into tableName (field1Name) values (fieldNamevalue1)

How do I get the list of scope_identities for a inset into select using Sql Server 2008?

I am trying to do a insert into select from a temp table. My problem is that I need to insert the items and also retrieve IDs of the inserted items in the insert statement.
How do I get the list of identity values that were inserted?
The code is below:
SELECT O.OrderID,
O.SingleAgreementID ,
O.OrderTypeID ,
O.OrderStatusID ,
O.Reference ,
O.CreateDate ,
O.ValidityDate ,
O.DeliveredDate ,
O.PathologyID ,
O.DiscountTypeID ,
O.DiscountAmount ,
O.ValidityDays ,
O.DeductibleTypeID ,
O.DeductibleAmount ,
O.LimitOrder ,
O.Comments ,
O.CreatedUserID ,
O.StartPeriodDate ,
O.EndPeriodDate ,
O.GenerationDay ,
O.ParentOrderID ,
O.CanceledDate ,
O.CanceledUserID INTO #TEMPORDERS
FROM dbo.[Order] O
WHERE O.GenerationDay = DAY(GETDATE() -1)
AND O.OrderTypeID = 2
AND #Yesterday BETWEEN CONVERT(VARCHAR(10),O.StartPeriodDate,111) AND CONVERT(VARCHAR(10),O.EndPeriodDate,111)
INSERT INTO dbo.[Order]
( SingleAgreementID ,
OrderTypeID ,
OrderStatusID ,
Reference ,
CreateDate ,
ValidityDate ,
DeliveredDate ,
PathologyID ,
DiscountTypeID ,
DiscountAmount ,
ValidityDays ,
DeductibleTypeID ,
DeductibleAmount ,
LimitOrder ,
Comments ,
CreatedUserID ,
StartPeriodDate ,
EndPeriodDate ,
GenerationDay ,
ParentOrderID ,
CanceledDate ,
CanceledUserID
)
SELECT TEMP.SingleAgreementID ,
TEMP.OrderTypeID ,
1 ,
TEMP.Reference ,
TEMP.CreateDate ,
GETDATE() + TEMP.ValidityDays,
NULL ,
TEMP.PathologyID ,
TEMP.DiscountTypeID ,
TEMP.DiscountAmount ,
TEMP.ValidityDays ,
TEMP.DeductibleTypeID ,
TEMP.DeductibleAmount ,
TEMP.LimitOrder ,
TEMP.Comments ,
'Orden Generada de manera automatica' ,
TEMP.StartPeriodDate ,
TEMP.EndPeriodDate ,
TEMP.GenerationDay ,
TEMP.OrderID ,
NULL ,
NULL
FROM #TEMPORDERS TEMP
--Get all Ids inserted HERE
SELECT SCOPE_IDENTITY();
--Get the IDs saved and insert the detail.
I don't know whether that is possible or not? Any ideas on this?
Thanks.
You can use Output to fill a table with th new ID's
Declare #OutputTable table(aNewid int)
Insert into Table
........
Output inserted.ID
Select ....
from InputTable
Try this one -
INSERT INTO dbo.[Order] (
SingleAgreementID
, OrderTypeID
, OrderStatusID
...
)
OUTPUT INSERTED.[IdentityColumn] INTO #temp1
SELECT t.SingleAgreementID
, t.OrderTypeID
, 1
...
FROM #TEMPORDERS t
SELECT * FROM #temp1

ORA 00937 while using INSERT INTO SELECT

When I am running the below insert into select statement, I get ORA 00937 because the below query cannot deal with one of the sub selects on APPLICATIONS table. I don't want to hardcode that value. Any suggestions?
Thanks in advance.
insert into CONFIGURATION_PARAMETER_VALUES
( ID
, NAME
, DESCRIPTION
, DATA_TYPE
, VALUE_STRING
, VALUE_INTEGER
, VALUE_DATE
, VALUE_FLOAT
, VALUE_TIMESTAMP
, APPLICATION_ID
, DELETED
)
select NVL(MAX(ID),0)+1
, 'Alert_Statuses_AllExceptNoStatus'
, 'Suspicious'
, 'String'
, 'RBS_EIM_AL_008'
, null
, null
, null
, null
, (select ID from APPLICATIONS where name = 'Rabobank v 1.0.0.0')
, 'N'
from CONFIGURATION_PARAMETER_VALUES
If it's not too late, I'd suggest implementing a SEQUENCE instead of counting. You may not get strict numeric order (there can be gaps), but you'll get a unique value every time:
CREATE SEQUENCE Config_Parm_Values_Seq START WITH <1 + your current max ID>;
Also note that your INSERT as it stands right now will behave as follows:
If there are no records in the table, it will insert nothing.
If there are records in the table, it will double the number of rows in your table every time you execute it.
So, even if you don't use the sequence, I'd consider a "plain old" INSERT instead of an INSERT ... SELECT. This example uses the sequence:
insert into CONFIGURATION_PARAMETER_VALUES
( ID
, NAME
, DESCRIPTION
, DATA_TYPE
, VALUE_STRING
, VALUE_INTEGER
, VALUE_DATE
, VALUE_FLOAT
, VALUE_TIMESTAMP
, APPLICATION_ID
, DELETED
) VALUES (
Config_Parm_Values_Seq.NEXTVAL -- Use seqname.nextval to get
-- the next value from the sequence
, 'Alert_Statuses_AllExceptNoStatus'
, 'Suspicious'
, 'String'
, 'RBS_EIM_AL_008'
, null
, null
, null
, null
, (select MAX(ID) from APPLICATIONS where name = 'Rabobank v 1.0.0.0')
, 'N')
The problem is in your SELECT statement where you are using SELECT NVL(MAX(ID), 0) + 1.
As you are using MAX function in the SELECT list, you must use a GROUP BY, which is not the solution here.
Use something like the following:
insert into CONFIGURATION_PARAMETER_VALUES
( ID
, NAME
, DESCRIPTION
, DATA_TYPE
, VALUE_STRING
, VALUE_INTEGER
, VALUE_DATE
, VALUE_FLOAT
, VALUE_TIMESTAMP
, APPLICATION_ID
, DELETED
)
select (SELECT MAX(ID) FROM configuration_parameter_values) + 1
-- above line instead of NVL(MAX(ID),0)+1
-- You can also put NVL function around the subquery.
, 'Alert_Statuses_AllExceptNoStatus'
, 'Suspicious'
, 'String'
, 'RBS_EIM_AL_008'
, null
, null
, null
, null
, (select ID from APPLICATIONS where name = 'Rabobank v 1.0.0.0')
-- Warning: The above subquery can generate a TOO_MANY_ROWS exception.
-- Use the solution in the other answer to avoid this.
, 'N'
from CONFIGURATION_PARAMETER_VALUES