I'm receiving a "Syntax error in INSERT INTO statement" when trying to submit this query.
INSERT INTO
Modified (ModifiedID, Username, Table, RecordID)
VALUES
('12','null','Accident','1')
All datatypes match in the database. If I run the query without inserting into "Table", it works. I don't know why, when I add the table string value to the query I receive the error.
Try this
INSERT INTO Modified (ModifiedID, Username, [Table], RecordID)
VALUES ('12','null','Accident','1')
In most SQL dialects, TABLE is a RESERVED WORD. The [] is Microsoft's delimiter and I believe backticks ` are used in MySQL
Also, do you really want 'null' as opposed to NULL, they have different meanings
In HSQLDB:
INSERT INTO Modified (ModifiedID, Username, "Table", RecordID)
VALUES (12, NULL, 'Accident', 1);
I am just going to assume that "key-ish" sounding fields, eg. ModifiedID and RecordID, are supposed to be INTEGER or BIGINT rather than, say, VARCHAR. DDL would be nice.
In MySQL:
INSERT INTO Modified (ModifiedID, Username, `Table`, RecordID)
VALUES ('12', NULL, 'Accident', '1')
Related
I work with an application that uses PostgreSQL 10.7. Part of the application allows you to bundle a group of of database objects and SQL statements into a package that you can later run when creating a Dev environment.
Each object and SQL statement has its own record in the database. I have to create over 1000 records so I am trying to create a script that will insert the SQL statements into the database for me.
I created my script but I am getting an error once Postgres sees the second "Value" command that is part of the record I am trying to insert.
Here is an example of what I am trying to do:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
'INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES
'LCMSMS','PEST',1);'
'CJONES',
'9/11/2019' );````
I am expecting it to be inserted but I am getting the following error. Can anyone guide me on how to "insert my insert statement"?
LINE 8: ...NAME,SQL_STMT,ADDED_BY,DATE_ADDED) VALUES ('LCMSMS...````
Your SQL statement contains emmbedded quotes, that clash with the surrounding quotes. You would need to double these quotes, like:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
'INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES (''LCMSMS'', ''PEST'', 1);'
'CJONES',
'9/11/2019' );````
As commented by mu is too short, another solution would be to use Postgres dollar quoting syntax. This saves you the effort of double quoting each and every embedded quote:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
$$INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES ('LCMSMS', 'PEST', 1);$$
'CJONES',
'9/11/2019' );````
I am trying to insert into a Bill table with 3 columns: BillID, FoodID and Count. No primary key because BillID and FoodID can be repeated.
I run this query:
insert into BillInfo (BillID, FoodID, Count)
values (3, 'SP05', 1)
It works fine in Microsoft Access (query view), but not in my Visual Basic project.
I tried different table, and it can insert okay, but not this table. I think something is wrong with violation key, but the exception yield
Syntax error in insert into statement
And I don't know how to check it. Please help me.
Dim query = "insert into BillInfo(BillID, FoodID, Count) values(3, 'SP05', 1)"
Dim result = DataProvider.Instance.executeNonQuery(query)
My DataProvider class basically can execute any query with any parameter.
I just tried hard code as an example
Count is a reserved word in any database engine that I can think about.
You should try to avoid such words in your database schema.
Anyway, if you want to use that word, then include it in square brackets
"Insert into BillInfo(BillID, FoodID, [Count]) values(3, 'SP05', 1)"
I'm following an older tutorial learning Postgres, so it's possible maybe something has changed since it was published. In the tutorial (using psql) I create a table then do some insert statements. Here is the tutorial and corresponding psql commands that cause error:
http://www.postgresqlforbeginners.com/2010/11/create-table-and-constraints.html
create table people(
id int PRIMARY KEY,
name varchar NOT NULL
);
insert into people(0,'Steve Jobs');
insert into people(1,'Mike Markkula');
insert into people(2,'Mike Scott');
insert into people(3,'John Sculley');
insert into people(4,'Michael Spindler');
insert into people(5,'Gil Amelio');
insert into people(6,'Mike Scott');
I get this error for each insert statement:
ERROR: syntax error at or near "0"
LINE 1: insert into people(0,'Steve Jobs');
^
I've tried copy pasting, capitalizing the sql commands (ie INSERT), running the command from shell outside of psql, adding spaces, using " instead of ' quotes... All result in the same errors. Has something changed or am I possibly doing something wrong?
The problem is the missing values (as noted in a comment).
I want to make some suggestions. First, whenever you use insert, you should always list the columns. This is especially important if you are learning the language -- you should be learning good habits.
Second, you don't need multiple inserts. A shorter way to insert multiple rows is:
insert into people (id, name)
values (0,'Steve Jobs'),
(1,'Mike Markkula'),
(2,'Mike Scott'),
(3,'John Sculley'),
(4,'Michael Spindler'),
(5,'Gil Amelio'),
(6,'Mike Scott');
And you should learn about serial. A more common way to write this code would be:
create table people (
id serial PRIMARY KEY,
name varchar NOT NULL
);
insert into people (name)
values ('Steve Jobs'),
('Mike Markkula'),
('Mike Scott'),
('John Sculley'),
('Michael Spindler'),
('Gil Amelio'),
('Mike Scott');
The id is assigned automatically by the database (starting at 1 rather than 0).
I should add: I am personally uncomfortable with having varchar without a length. This is perfectly fine in Postgres, but some databases would interpret it as varchar(1).
I am using Oracle databases. I have an sql table PS_Z_STAGE_TEST_JE that has three fields (EMPLID, LAST_NAME, FIRST_NAME).
I am trying to do a select statement that will pull many EMPLIDs from sql table:ps_vc_plan_mem and insert them into the EMPLID column while leaving the other two fields (LAST_NAME and FIRST_NAME) null.
Below is my SQL but it will say
Cannot insert null Value into LAST_NAME
when I try to run it.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID)
SELECT DISTINCT(emplid)
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
The only obvious problem that I see with your query is the asterisk at the end:
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE(EMPLID)
SELECT DISTINCT emplid
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS', 'PNC-SO', 'PNC-ESPP');
Note that distinct is not a function. It is a modifier on select, so the parentheses don't mean anything.
The error message is exactly what it says it is. Your last_name column must be defined as not null. Therefore, you can't insert a null into it. Since you didn't define what to insert into the column in your insert, it tries to insert null by default and fails.
You must insert something into last name. I would suggest either a default string or an empty string if you can't get an actual last name to insert.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID, LAST_NAME)
SELECT DISTINCT(emplid), 'N/A'
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
Alternatively, you could alter your table so that last_name is nullable.
There is a asterisk at the end of your SQL statement. Please remove and retry.
If [sysadm].[PS_Z_STAGE_TEST_JE] table has a PK, you might want truncate the table before running the statement.
I have a CSV file like
1,hello,13
2,world,14
3,ciao,26
I'm trying to use CSVREAD function to read this file into database, like this
insert into my_table( id, message, code ) values (
select convert( "id",bigint ), "message", convert( "code", bigint)
from CSVREAD( 'myfile.csv', 'id,message,code', null )
);
For some reason I keep on getting SQL error stating that the column count does not match.
The table is created with Hibernate/GORM and contains the fields I try to insert into.
The select itself seems to work, or at least it does not cause any errors when executed alone. What's wrong with my statement?
You have used
insert into my_table(...) values (select ...)
but you should use, as documented in the SQL railroad diagrams,
insert into my_table(...) select ...
Actually, for H2, it is a bit faster if you create the table as follows, but I understand it is not always possible:
create table my_table(...) as select ...