I have a simple table called source with 3 columns (source_id, name, tenant_id) and some data in it.
I a trying to insert a data into the table by checking its existence in the table. But I am getting this error ..any idea how to resolve
The Query -
INSERT INTO moa.source(source_id, name, tenant_id)
SELECT ((select max(source_id)+1 from moa.source), 'GE OWS - LHR', 1)
WHERE NOT EXISTS(SELECT 1 FROM moa.source where name = 'GE OWS - LHR');
The Error :
ERROR: INSERT has more target columns than expressions
LINE 1: INSERT INTO moa.source(source_id, name, tenant_id)
^
HINT: The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?
SQL state: 42
The Table:
Sorry..figured it out..there should be no paranthesis after Select
Related
i try update data from another table using PostgreSQL 9.6 following the document and stackoverflow advice with this query
1. this query is for finding the id_vertex of geom that close to lokasi_esb.geom. You can ignore this one, it works properly
CREATE TEMP TABLE temp1 AS
WITH kuery2 as(
SELECT id_esb, id_vertex, distant, rank() OVER (PARTITION BY id_esb ORDER BY distant asc) as ranked FROM table vertex)
select id_esb, id_vertex, distant, ranked
from kuery2
where ranked=1;
2. this query to update the lokasi_esb table with id_vertex_nearest column without excluded table. ////i already know it's wrong and i update on the number 3
INSERT INTO lokasi_esb(id_esb, id_vertex_nearest)
select id_esb,id_vertex
from temp1
ON CONFLICT (id_esb) DO UPDATE
SET lokasi_esb.id_vertex_nearest = temp1.id_vertex;
i got this error
ERROR: missing FROM-clause entry for table « temp1 »
SQL state: 42P01
Character: 634
3. this query to update the lokasi_esb table with id_vertex_nearest column with excluded table
INSERT INTO lokasi_esb(id_esb, id_vertex_nearest)
select id_esb,id_vertex
from temp1
ON CONFLICT (id_esb) DO UPDATE
SET lokasi_esb.id_vertex_nearest = excluded.id_vertex;
igot this error(transleted from indonesia)
ERROR: column excluded.id_vertex not yet exist
SQL state: 42703
Character: 634
So can anybody can help me figure out what happened here?
The column names from the "excluded" record refer to the columns of the target table. And the target column in the SET expression must not be prefixed with the table name (because you can't update a different table anyway)
So you need to use:
SET id_vertex_nearest = excluded.id_vertex_nearest
I want to insert a new record if the record is not present in the table
For that I am using below query in Teradata
INSERT INTO sample(id, name) VALUES('12','rao')
WHERE NOT EXISTS (SELECT id FROM sample WHERE id = '12');
When I execute the above query I am getting below error.
WHERE NOT EXISTS
Failure 3706 Syntax error: expected something between ')' and the 'WHERE' keyword.
Can anyone help with the above issue. It will be very helpful.
You can use INSERT INTO ... SELECT ... as follows:
INSERT INTO sample(id,name)
select '12','rao'
WHERE NOT EXISTS (SELECT id FROM sample WHERE id = '12');
You can also create the primary/unique key on id column to avoid inserting duplicate data in id column.
I would advise writing the query as:
INSERT INTO sample (id, name)
SELECT id, name
FROM (SELECT 12 as id, 'rao' as name) x
WHERE NOT EXISTS (SELECT 1 FROM sample s WHERE s.id = x.id);
This means that you do not need to repeat the constant value -- such repetition can be a cause of errors in queries. Note that I removed the single quotes. id looks like a number so treat it as a number.
The uniqueness of ids is usually handled using a unique constraint or index:
alter table sample add constraint unq_sample_id unique (id);
This makes sure that the database ensures uniqueness. Your approach can fail if two inserts are run at the same time with the same id. An attempt to insert a duplicates returns an error (which the exists can then avoid).
In practice, id columns are usually generated automatically by the database. So the create table statement would look more like:
id integer generated by default as identity
And the insert would look like:
insert into sample (name)
values (name);
If id is the Primary Index of the table you can use MERGE:
merge into sample as tgt
using VALUES('12','rao') as src (id, name)
on src.id = tgt.id
when not matched
then insert (src.id,src.name)
I have a table projects in postgreSQL that looks like below
id. project_name. col3. col4
1111. test. ... ...
I want to insert a new row for a new id only if the id does not already exist. So I wrote below query
INSERT INTO projects(id, project_name)
VALUES("1234", "Test_Project")
ON CONFLICT (id)
DO NOTHING
But it throws me an error
Query 1 ERROR: ERROR: column "1234" does not exist
LINE 2: VALUES("1234", "Test_Project_...
Please suggest.
** EDIT**
The id column in the table is a uuid and not unique. There can be multiple rows with the same id. Based on GMBs suggestion, I tried below
INSERT INTO projects(id, project_name)
VALUES('1234', 'Test_Project')
ON CONFLICT (create unique index on projects(id))
DO NOTHING
I get below error with it
Query 1 ERROR: ERROR: syntax error at or near "create"
LINE 3: ON CONFLICT (create unique index on projects(id...
I am new to this so I am sure missing something obvious.
Use single quotes for literal strings. Double quotes stand for identifiers (such as column names or table names) - hence the error that you are getting:
INSERT INTO projects(id, project_name)
VALUES('1234', 'Test_Project')
ON CONFLICT (id)
DO NOTHING
That said, I would suspect that id is of integer datatype. If so, don't quote it at all:
INSERT INTO projects(id, project_name)
VALUES(1234, 'Test_Project')
ON CONFLICT (id)
DO NOTHING
I've created sequence by following statement:
CREATE SEQUENCE MAIN.MY_SEQUENCE START WITH 1 INCREMENT BY 1 CACHE 50;
And table by following statement:
CREATE TABLE MAIN.EMPLOYEES(
ID INTEGER NOT NULL,
NAME VARCHAR(512),
EMAIL VARCHAR(254),
PRIMARY KEY (ID)
)
Now when I try to insert a new record by using following statement:
INSERT INTO MAIN EMPLOYEES (ID, NAME, EMAIL)
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 1', 'email1#example.com') UNION ALL
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 2', 'email2#example.com')
I get an error:
"NEXTVAL FOR MAIN.MY_SEQUENCE.NEXTVAL" cannot be specified in this context.. SQLCODE=-348, SQLSTATE=428F9, DRIVER=4.17.30
When I try to insert a single row everything works fine.
I have found a list of restrictions on using NEXT VALUE here but here not mentioned my case or I couldn't find it.
My question is it possible to insert multiple rows by using ID from sequence, and if yes, how can I achieve it?
It does list your case. The documentation contains this:
The NEXT VALUE expressions cannot be specified in the following contexts:
...
•SELECT statement for which the outer SELECT is combined with another SELECT statement using a set operator such as UNION, EXCEPT, or INTERSECT
....
(emphasis mine) This statement isn't exhaustive, and because UNION ALL is considered a set operation, the operation is excluded.
This should be fixable - I'm a little surprised you wrote the statement the way you did; DB2 allows you to comma-separate data rows. That is, the following should be valid:
INSERT INTO MAIN.EMPLOYEES (ID, NAME, EMAIL)
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 1', 'email1#example.com'),
(MAIN.MY_SEQUENCE.NEXTVAL, 'Name 2', 'email2#example.com')
I'm trying to insert values in my database
This is the statement I try to execute:
insert into leverancier (id,naam,straat,nr,postcode,plaats,telefoon)
values (1,"stef","bosstraat",88,9240,"Zele",null);
I get the following error:
ERROR 42X04: Column 'stef' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'stef' is not a column in the target table.
What is the problem?
To insert a string, like "stef", don't use double quotes but single quotes: 'stef'. Here's how the statement should be:
INSERT INTO leverancier
(id, naam, straat, nr, postcode, plaats, telefoon)
VALUES
(1,'stef', 'bosstraat', 88, 9240, 'Zele', NULL);
The error you get Column 'stef' is either not in any table ... is because double quotes are used for table and column names. So reading "stef", the parser assumes you are referring to a column named stef.