PostgreSQL insert or return id [duplicate] - sql

This question already has answers here:
Get or else insert in PostgreSQL
(1 answer)
Function to select existing value or insert new row
(2 answers)
Doing either SELECT or INSERT
(2 answers)
Closed 1 year ago.
I'm trying to insert or return id of existing value in case of uniqueness conflict (the data is already there).
I'm guessing it could be something like this, but can't quite get it to return the actual ID
INSERT INTO my_table (my_column) VALUES ('my value')
ON CONFLICT DO NOTHING
RETURNING ID;
where my_column has a unique constraint.
Any ideas?

Related

TSQL get identity column value for the inserted rows without using cursor [duplicate]

This question already has answers here:
Sql Server return the value of identity column after insert statement
(7 answers)
How to get the identity of an inserted row?
(15 answers)
Closed 28 days ago.
I have a result set like below, RefId column will be empty initially. I need to loop through this result set insert values into one table ex : TableA , get the inserted row's identity (primary key) column value of the inserted rows and do some more manipulations.
I tried with cursors, since they are not recommended to use, how else can I do this in tsql ?
Result set query :
Insert into TableA(column1,column2,column3)
select column1, column2, column3 from Table B -- I want the identity values of the rows inserted into TableA,
I need them in another result set/ temp table or table variable along with the source values (TableB's select list values)

postgresSQL: column doesn't exist, when trying to insert into table [duplicate]

This question already has answers here:
What is the difference between single quotes and double quotes in PostgreSQL?
(3 answers)
PostgreSQL - insert statement error, thinks value to insert is a column name
(1 answer)
INSERT COMMAND :: ERROR: column "value" does not exist
(2 answers)
postgres column "X" does not exist
(1 answer)
Closed 1 year ago.
when I run this in terminal:
INSERT INTO signup (username, user_password) VALUES("John", "dqw1");
It gives me: ERROR: column "John" does not exist.
What could be the problem here? this doesn't even make sense, the column name is username. "John" is just a value, it shouldn't exist before.
The issue is with the double quotes - postgres is interpreting them as "delimited identifiers" (i.e. the name of an object, such as a column in a table).
So instead of this:
INSERT INTO signup (username, user_password) VALUES("John", "dqw1");
Do this:
INSERT INTO signup (username, user_password) VALUES('John', 'dqw1');

PostgresSql error: more than one row returned by a subquery used as an expression [duplicate]

This question already has answers here:
Insert into ... values ( SELECT ... FROM ... )
(27 answers)
Closed 3 years ago.
i am running this query where in rel_branding there are more than 1 rows and i want to insert brandingid of every record in rel_branding_permission.
This is for PostgresSQL.
insert into rel_branding_permission (brandingid,permissionid)
values((select brandingid from rel_branding), 404);
I want to insert this query in my DB as for all branding ids
You should be using an INSERT INTO ... SELECT here, something like this:
INSERT INTO rel_branding_permission (brandingid)
SELECT brandingid
FROM rel_branding
WHERE <some condition>
You would probably want to include more than one column in the insert into the rel_branding_permission table, and you may modify the above to fit your need by adding the additional columns to both the INSERT and the SELECT.

How get id after inserting a new row of data into the table? [duplicate]

This question already has answers here:
PostgreSQL function for last inserted ID
(14 answers)
Closed 4 years ago.
I have:
Insert Into(name)
Value('Anton')
How do I get the newly created id?
Insert a single row into table distributors, returning the sequence
number generated by the DEFAULT clause:
Code
INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
RETURNING did;
The optional RETURNING clause causes INSERT to compute and return
value(s) based on each row actually inserted. This is primarily useful
for obtaining values that were supplied by defaults, such as a serial
sequence number. However, any expression using the table's columns is
allowed. The syntax of the RETURNING list is identical to that of the
output list of SELECT.
Reference:
https://www.postgresql.org/docs/9.1/static/sql-insert.html
https://stackoverflow.com/a/2944481/7793817
You use the RETURNING keyword
https://www.postgresql.org/docs/9.5/static/dml-returning.html
From the doc :
INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id;

How to insert an empty line to SQL table? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inserting rows into a table with one IDENTITY column only
I have a SQL table with just one column. Column is an autoincrement field like:
Id INT IDENTITY
I need to insert a rows to this table (this is a bulk/dummy table temporarily):
INSERT INTO my_table VALUES ()
But I'm got a syntax error.
How can I do this?
INSERT INTO my_table DEFAULT VALUES
Try to insert explicit null values:
INSERT INTO my_table VALUES (NULL, NULL);