postgresql INSERT ... RETURNING query keeps failing? - postgresql

The error message is kinda vague... The query below works on one server, but fails on another:
INSERT INTO searches (status, uid, datecreated, searchname, options)
VALUES (0, 1, NOW(), 'TEST', '16') RETURNING sid;
And I get this error:
Query failed: ERROR: syntax error at or near "RETURNING"
Am I missing something here?

Query certainly looks right - assuming the field sid does exist (I take it it's a SERIAL field).
Are you sure you're using a PostgreSQL version that has support for RETURNING? You need version 8.2 or newer for this.

Related

Query returns Error despite being executed succesfully (Robot Framework / JayDeBeApi)

Using the Keyword Query from the Robot Framework DatabaseLibrary JayDeBeApi in conjunction with DB2 like this: ${results}= Query CREATE TABLE SCHEMANAME.TEST_TEMP (id BIGINT, name VARCHAR(25)) is being executed (table exists afterwards).
But nevertheless RobotFramework throws a FAIL and ${results} contains the Message DatabaseError: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-601, SQLSTATE=42710, SQLERRMC=SCHEMANAME.TEST_TEMP;TABLE, DRIVER=4.14.122 and often even a very simple Message Error after running the same statement.
Running the query above (copy/paste) directly within a database SQL window doesn't return any errors.
How is it possible, in RobotFramework the query is executed successfully but nevertheless an error is thrown?
The error SQLCODE=-601 means that you are trying to create an object that already exists. So when you say that the table exists afterwards, it means that it existed before you ran the statement. I don't know the framework you are using, but the explanation by #pavelsaman in comment seems to be a very likely cause.

Insert with Array in Values with On Conflict

I can't seem to get this postgresql execute to work.
INSERT INTO table (x, y)
VALUES ('somestring', '{0.123, -0.123}')
ON CONFLICT DO NOTHING RETURNING id;
I get the error:
ProgrammingError: (psycopg2.ProgrammingError) syntax error at or near "ON"
I've tried a variety of things, and I'm not very well versed in postgresql or sql in general. I'm on version 9.5.1 of Postgresql.
I'm also using SQLAlchemy (Python Lib). Is there a way to check what version of Postgresql my SQLAlchemy is using?
You have to specify what may cause the conflict.
Example from the docs:
INSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')
ON CONFLICT (did) DO NOTHING;
edit: I am referring to the (did) part

ERROR In Sequences query if postgres

I create one sequence in postgres and fire one query which is mentioned below
SELECT M_PRODUCTSEQ.NEXTVAL from DUAL;
but it gives me the below error:
ERROR: relation "dual" does not exist.
Kindly help me out. How can i made the relation with dual?
PostgreSQL does NOT support the from DUAL syntax. It does however make the from portion of a query like this optional, so getting the next value (nextval) of a sequence you would do something like this:
SELECT nextval('m_productseq');

Can't enter date into postgres field with datatype reltime

I'm trying to make an insert into postgres 8.4.13
insert into my_table (id, hour_memo) values (1,'17:30:00.000000 +01:00:00');
hour_memo is 'reltime datatype'
During the execution of the insert task i have this trouble:
ERROR: invalid input syntax for type reltime: "17:30:00.000000 +01:00:00"
I have absolutely no idea on how to do this?
The answer is that reltime doesn't support time zones, so the "+01..." thing is breaking it. Still - using reltime type is bad idea, and should be replaced by some normal type.

Why wont this sql server CE command work?

I have the command:
INSERT INTO tbl_media
(DateAdded) VALUES (GetDate())
SELECT CAST(##Identity AS int)
It works fine against a standard sql db but not against a CE db I get the following error:
SQL Execution Error.
Executed SQL statement...
Error Source: SQL Server Compact ADO.NET Data Provider
Error Message: There was an error parsing the query. [Token line number = 2, Token line offset = 31, Token in error = )]
Shame the error isn't more useful anyone know what could be going on?
Cheers
UPDATE::::::
After much messing around with visual studio editor (rubbish) I downloaded dataport and read the MSDN. It seems there are 2 problems...
1) SELECT CAST(##Identity AS int) is not valid sql
SELECT ##Identity is
2) SqlCe server does not like it when I put these two commands together:
INSERT INTO tbl_media
(DateAdded) VALUES (getdate())
SELECT ##Identity
If I do the insert and select at different times then it works. So how do I get round this? I cant do it at different times I need to know the ID of the objects as I create them!!!
UPDATE 2:
According to the very helpful Erik E you cant do 2 statements at the same time. So the following parses as correct but wont work:
INSERT INTO tbl_media
(DateAdded) VALUES (getdate());
SELECT ##Identity;
So what I really want to know is how do I guarantee that identities wont get mixed up when adding records?
I.e. what if someone creates a record while someone is getting the identity for one they have just inserted?
You have an extra ) I dont know if that will fix your error but look at VALUES you have
VALUES(GETDATE())) '<-- one ) extra.
Change it to this:
INSERT INTO tbl_media(DateAdded)
VALUES (GetDate())
SELECT CAST(##Identity AS int)
You can only run a single SQL statement per ExecuteNonQuery call. So you must spilt in 2 calls.