Select and insert on the same table - sql

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

Related

I can't find where i forgot to use comma in mapper of Spring

I tried to fix my root causes but I can't find where is problem.
Console says i forgot comma, but I don't know well where is this
### SQL: INSERT INTO TEST_MEMBER1 ( MEMBER_ID , MEMBER_PASSWORD , MEMBER_NAME , MEMBER_AGE , MEMBER_ADDR , TO_CHAR(MEMBER_BIRTHDATE, 'YYYY-MM-DD') AS BIRTHDAY_DATE ) VALUES( ? , ? , ? , ? , ? ,? )
### Cause: java.sql.SQLSyntaxErrorException: ORA-00917: 누락된 콤마
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00917: 누락된 콤마
]을(를) 발생시켰습니다.
Error : 917, Position : 128, Sql = INSERT INTO TEST_MEMBER1 (
MEMBER_ID
, MEMBER_PASSWORD
, MEMBER_NAME
, MEMBER_AGE
, MEMBER_ADDR
, TO_CHAR(MEMBER_BIRTHDATE, 'YYYY-MM-DD') AS BIRTHDAY_DATE
)
VALUES(
:1
, :2
, :3
, :4
, :5
,:6
), OriginalSql = INSERT INTO TEST_MEMBER1 (
MEMBER_ID
, MEMBER_PASSWORD
, MEMBER_NAME
, MEMBER_AGE
, MEMBER_ADDR
, TO_CHAR(MEMBER_BIRTHDATE, 'YYYY-MM-DD') AS BIRTHDAY_DATE
)
VALUES(
?
, ?
, ?
, ?
, ?
,?
), Error Msg = ORA-00917: 누락된 콤마
You currently have a to_char() function call in the column list; it should be in the values clause instead:
INSERT INTO TEST_MEMBER1 (
MEMBER_ID
, MEMBER_PASSWORD
, MEMBER_NAME
, MEMBER_AGE
, MEMBER_ADDR
, MEMBER_BIRTHDATE
)
VALUES(
?
, ?
, ?
, ?
, ?
, TO_CHAR(?, 'YYYY-MM-DD')
)
But that conversion implies that your MEMBER_BIRTHDATE is a string (VARCHAR2) column. You should store data in columns with the correct data type, so really that should be a DATE column, and then when you bind a date value it won't need to be converted.

Column name checking null still giving null error

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;

Insert value into table

I have below SQL query, am trying to insert data into test table, but I got another requirement that I need to insert the employee number along with his/her name.
Example
firstname : 71853-osama
My question is, How I can insert two values into one attribute
I tried this
badgeno +'-'+ convert(nvarchar(100),cEmpname) as cEmpname
but it didn't work
insert into PT (
[FirstName]
,[LastName]
,[FirmID]
,[Note]
,[City]
,[ThirdPartyId]
,[RegisteredBy]
,[Registered]
,[LastUpdatedBy]
,[LastUpdated]
)
SELECT distinct
convert(nvarchar(100),cEmpname) as cEmpname
,convert(nvarchar(100),cJobTitle) as cJobTitle
,'2' as FirmID
,convert(nvarchar(500),sort1) as sort1
,convert(nvarchar(255),cnationality) as cnationality
, badgeno as 'ThirdPartyId'
,'admin' as RegisteredBy
,CURRENT_TIMESTAMP as Registered
,'admin' as LastUpdatedBy
, CURRENT_TIMESTAMP as LastUpdated
FROM [TrailBlazerNG].[dbo].[payper] where lactive = '1'
and not exists ( select 1 from PT where payper.badgeno = PT.ThirdPartyId)
Try below.
insert into PT ([FirstName],[FirmID],[Note],[City],[ThirdPartyId],[RegisteredBy],[Registered],[LastUpdatedBy],[LastUpdated])
SELECT distinct
convert(nvarchar(100),badgeno)+'-'+convert(nvarchar(100),cEmpname) as cEmpname
,convert(nvarchar(100),cJobTitle) as cJobTitle
,'2' as FirmID
,convert(nvarchar(500),sort1) as sort1
,convert(nvarchar(255),cnationality) as cnationality
, badgeno as 'ThirdPartyId'
,'admin' as RegisteredBy
,CURRENT_TIMESTAMP as Registered
,'admin' as LastUpdatedBy
, CURRENT_TIMESTAMP as LastUpdated
FROM [TrailBlazerNG].[dbo].[payper] where lactive = '1'
and not exists ( select 1 from PT where payper.badgeno = PT.ThirdPartyId)

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