Incorrect syntax near the keyword 'where' - sql

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'

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.

UPSERT for "INSERT INTO tab SELECT * FROM another_tab"

How would I do "UPSERT" (INSERT OR UPDATE) into SQLite table when inserting multiple rows from another table.
I've tried:
INSERT INTO tab_name
SELECT * FROM tmp
ON CONFLICT(id)
DO UPDATE SET
val = excluded.val;
But it gives me:
syntax error near "DO"
What would be the correct and the most efficient way to achieve that?
You might have hit a documented trap called parsing ambiguity :
When the INSERT statement to which the UPSERT is attached takes its values from a SELECT statement, there is a potential parsing ambiguity. The parser might not be able to tell if the "ON" keyword is introducing the UPSERT or if it is the ON clause of a join. To work around this, the SELECT statement should always include a WHERE clause, even if that WHERE clause is just "WHERE true".)
So, does this work better?
INSERT INTO tab_name
SELECT * FROM tmp WHERE true
ON CONFLICT(id) DO UPDATE SET val = excluded.val;

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 into using Values

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.

SQL INSERT with sub query

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.
Heres what I came up with but I know is wrong..
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT #ModelId, VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId)
Essentially I want to provide the value for VehicleModelId through #ModelId, but it won't let me use it outside of the select statement.
Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT #ModelId,VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId