insert into using Values - sql

I am am using sql server 2005 and doing a simple insert into and getting an incorrect syntax error. I See nothing wrong with my code Can someone give me some ideas what could be wrong with it?
insert into inonhd
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV109','1','11','01','1'),
('CRV110','0','11','01','0')
the error is Incorrect syntax near ','.

You must add each row in separate command.
insert into inonhd
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV109','1','11','01','1')
and:
insert into inonhd
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV110','0','11','01','0')

It is really important to note that the syntax in the question is fine for more recent versions of SQL Server. This is acceptable:
insert into inonhd(fpartno, fpartrev, flocation, fonhand, fcudrev)
Values ('CRV109','1','11','01','1'),
('CRV110','0','11','01','0');
If you want to do this in one statement, you can use select . . . union all:
insert into inonhd(fpartno, fpartrev, flocation, fonhand, fcudrev)
select 'CRV109','1','11','01','1' union all
select 'CRV110','0','11','01','0';
Of course, multiple inserts are another possibility.

Related

Subqueries for PostgreSQL returns syntax error near select

I am a beginner to database, and I am trying to run a subquery to insert data from one table to another one with identical schema.
insert into tbl_technologies_used (t_name_tech, t_category_tech, i_rating)
values (
select t_name_tech, t_category_tech, i_rating
from tbl_technologies_proposed
);
But I got this error:
ERROR: syntax error at or near "select"
LINE 1: ... (t_name_tech, t_category_tech, i_rating) values (select t_n...
How can I fix this issue? I have checked my code again and again, but I can't find out the error.
If the source of an INSERT is a SELECT, you can't use VALUES:
insert into tbl_technologies_used (t_name_tech, t_category_tech, i_rating)
select t_name_tech, t_category_tech, i_rating
from tbl_technologies_proposed
The values clause provides a static set of rows which is not needed as the rows to be inserted come from your SELECT statement.

Sybase interacting with mybatis

Im unable to insert multiple records in a single transaction. Im using foreach in mapper and I'm getting incorrect syntax near ','.
I googled and found, in sybase it's not possible to insert multiple rows in sybase.
Insert statement looks like:
insert into Student(id,name)
values (1, Jon), (2,mike),(3,sam)
Comma next to value statement, is creating issue.

Oracle SQL command not properly ended

I am doing a simple insert and am stumped, I'm new to oracle and unsure of what the issue is. I don't have the table structure so I am guessing that most of the fields are character except the dates.
Anyway here is my query, can anyone find the issue?
INSERT INTO PHANTOM_BOXES (CARRIER_CODE,CARRIER_TRACKING_NO,SENT_DATE,SEND_COST,
RECEIVED_DATE,REC_COST, COMMENTS,SHIPPING_TECH,RECEIVING_TECH)
VALUES ('1','11',TO_DATE('2016-02-04','YYYY-MM-DD'),'1',
TO_DATE('2016-02-04','YYYY-MM-DD'),'1','1','26437','0')
WHERE BOX_NO = '6738'
NO where dude.what is ther where for.
INSERT INTO PHANTOM_BOXES (CARRIER_CODE,CARRIER_TRACKING_NO,SENT_DATE,SEND_COST,
RECEIVED_DATE,REC_COST, COMMENTS,SHIPPING_TECH,RECEIVING_TECH,BOX_NO)
VALUES ('1','11',TO_DATE('2016-02-04','YYYY-MM-DD'),'1',
TO_DATE('2016-02-04','YYYY-MM-DD'),'1','1','26437','0','6738')
WHERE BOX_NO = '6738'
INSERT statement cannot have a WHERE clause, makes no sense.
Simply do INSERT INTO..VALUES:
INSERT INTO PHANTOM_BOXES (CARRIER_CODE,CARRIER_TRACKING_NO,SENT_DATE,SEND_COST,
RECEIVED_DATE,REC_COST, COMMENTS,SHIPPING_TECH,RECEIVING_TECH)
VALUES ('1','11',TO_DATE('2016-02-04','YYYY-MM-DD'),'1',
TO_DATE('2016-02-04','YYYY-MM-DD'),'1','1','26437','0')
Where Clause is used for filter and applying condition Rows which were already present in Table.
Seems you are trying to update the values for WHERE BOX_NO = '6738'
For this you have to use Update Statement
Update PHANTOM_BOXES
Set CARRIER_CODE='1',
CARRIER_TRACKING_NO='11',
SENT_DATE=TO_DATE('2016-02-04','YYYY-MM-DD'),
SEND_COST='1',
RECEIVED_DATE=TO_DATE('2016-02-04','YYYY-MM-DD'),
REC_COST='1',
COMMENTS,SHIPPING_TECH='26437',
RECEIVING_TECH='0';

insert multiple rows in single SQL

I am trying to insert multiple rows with SQL statement.
For that i refered this question.
According to top scored answer in this qestion, i made following query:
INSERT INTO login
(LogInID,Password)
UNION ALL
SELECT 'Name1','pass1'
UNION ALL
SELECT 'Name2','pass2'
But when i try to execute this one, it gives me error:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'UNION'.
What can be the mistake in this query?
Is this a wrong approach?
Please help me.
NOTE: I am using SQL SERVER 2005
you have to remove UNION ALL before the first SELECT.
INSERT INTO login (LogInID,Password)
SELECT 'Name1','pass1'
UNION ALL
SELECT 'Name2','pass2'
Even though it does not provides an answer to your original question I think it's worth knowing that SQL Server provides another syntax using the VALUES syntax:
insert into login values
('Name1','pass1'),
('Name2','pass2'),
('Name3','pass3')

Incorrect syntax near the keyword 'where'

insert into product (CategoriesId) values (2) where Categories=' ab '
error is
Incorrect syntax near the keyword 'where'.
i cannot understand please help me
You can not have a where statement on the insert. If you are using a table then you can.
INSERT INTO product (CategoriesId) values (2)
Or like this:
INSERT INTO product (CategoriesId)
SELECT CategoriesId
FROM someTable
WHERE someTable.Categories=' ab '
Or if you have existing rows and want to UPDATE them. Then do this:
UPDATE product SET CategoriesId=2 WHERE Categories='ab'
If you want to update the username or in the main while you are not inserting the record. so use update query instead of insert query and for insert query Where clause is not used. Try this you can get your ans.. Good luck.
You can not use where clause with insert , You should use update like this
update product set CategoriesId = 2 where Categories='ab'