Error occurs: "Incorrect syntax near ' , ' " - sql-server-2012

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)

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;

Selection order for an Insert Into SQL Server 2008 R2

I have a query in which I create a table and then insert data into it. My question is this, I have one column where I need to make the data distinct, my output seems to be working correctly but I would like clarification on if this is an acceptable practice.
Here is what my query looks like:
DECLARE #T1 TABLE(
MRN VARCHAR(20)
, [ENCOUNTER] VARCHAR(200)
, ...
, ...
, ...
)
INSERT INTO #T1
SELECT
A.Med_Rec_No
, A.[VISIT ID]
, ...
, ...
, ...
)
FROM (
SELECT DISTINCT PAV.PTNO_NUM AS [VISIT ID] -- MUST I CHANGE THE ORDER?
, PAV.Med_Rec_No -- MUST I CHANGE THE ORDER?
, ...
, ...
, ...
)
Does the INSERT INTO #T1 SELECT take care of the ordering for me, meaning does it really matter what order I SELECT items in the FROM statement as long as my INSESRT INTO matches what my DECLARE #T1 TABLE says?
Thank you,
When doing INSERT INTO FROM SELECT you MUST match order of columns in SELECT statement as they are in INSERT statement.
Now on other hand you not required to match column order in INSERT statement to what is in CREATE TABLE.
It is always recommended to specify COLUMN in INSERT statement. Otherwise you assuming that what you selecting matches column order in table that you are inserting into.
In your case you should modify your query like so.
INSERT INTO #T1 (MRN, Encounter)
SELECT
A.Med_Rec_No
, A.[VISIT ID]
, ...
, ...
, ...
)
FROM (
SELECT DISTINCT PAV.PTNO_NUM AS [VISIT ID]
, PAV.Med_Rec_No
, ...
, ...
, ...
)
as alternative you can modify order of column in INSERT clause
INSERT INTO #T1 (Encounter,MRN)
SELECT
A.[VISIT ID]
,A.Med_Rec_No
, ...
, ...
, ...
)
FROM (
SELECT DISTINCT PAV.PTNO_NUM AS [VISIT ID]
, PAV.Med_Rec_No
, ...
, ...
, ...
)

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