Questions tagged [sql-insert]

The SQL INSERT statement allows you to insert a single or multiple rows into a table.

SQL INSERT statement adds one or more rows to a single table in a relational database.

The basic INSERT values syntax is:

INSERT INTO table [ (column1 [, column2, column3 ... ]) ]
VALUES (value1 [, value2, value3 ... ]);

The INSERT SELECT syntax is:

INSERT INTO table [ (column1 [, column2, column3 ... ]) ]
SELECT c1 [, c2, c3 ... ] FROM table

Reference

For questions regarding SQL INSERT statement use this tag instead of .

5071 questions
13
votes
2 answers

generate_series() - how to use it to populate multiple columns in a table?

I have a table with the following structure: widgetnumber - text dd - text refnumber - text widgetnumber and refnumber need to be populated with the same value... one that's generated using generate_series. dd will be a static value that's set…
Happydevdays
  • 1,982
  • 5
  • 31
  • 57
13
votes
5 answers

Best practices for inserting/updating large amount of data in SQL Server 2008

I'm building a system for updating large amounts of data through various CSV feeds. Normally I would just loop though each row in the feed, do a select query to check if the item already exists and insert/update an item depending if it exists or…
Mark Clancy
  • 7,831
  • 8
  • 43
  • 49
13
votes
3 answers

insert string which includes quotes in oracle

How can I insert string which includes quotes in oracle? my code is INSERT INTO TIZ_VADF_TL_MODELS (name) VALUES ('xxx'test'yy'); if I use INSERT INTO TIZ_VADF_TL_MODELS (name) VALUES ("xxx'test'yy"); I get identifier is too long error because…
neverwinter
  • 810
  • 2
  • 15
  • 42
13
votes
3 answers

Why I am getting an error when using w3school tutorial?

I've been looking at this code for a while now and I can't see where the problem is. I have been reading the whole of StackOverflow and still can't see where my error is.…
Blue
  • 261
  • 1
  • 3
  • 14
12
votes
2 answers

INSERT COMMAND :: ERROR: column "value" does not exist

I'm using postgresql and I'm trying to insert data into a table users. When I do that using INSERT INTO users (user_name, name, password,email) VALUES ("user2", "first last", "password1", "user2@gmail.com" ); I get the following error: ERROR: …
12
votes
2 answers

Dapper multi insert returning inserted objects

Using Dapper I would like to implement a method that takes an IEnumerable of objects of type User. Now, User looks as follows: public class User { public int UserId { get; internal set; } public DateTime DateCreated { get; internal set; } …
ackh
  • 1,648
  • 2
  • 18
  • 36
12
votes
5 answers

MYSQL inserting multiple rows unexpectedly

I'm having an issue and was hoping that someone could help me out. My issue is that whenever I run the code in the "MainFile", It outputs the two ID's that it has added the entries at, but then when I view my database I have six entries instead of…
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
12
votes
1 answer

Generate DEFAULT values in an UPSERT emulated with a CTE

Writable CTEs to emulate an UPSERT in PostgreSQL seem quite elegant. (Until we get actual upsert/merge in Postgres. See: https://stackoverflow.com/a/8702291/558819) Still working with PostgreSQL 9.3, there is one problem: how can I insert the…
Aktau
  • 1,847
  • 21
  • 30
12
votes
1 answer

Fastest way to update a MySQL table if row exists else insert. More than 2 non-unique keys

I have the following table structure: CREATE TABLE IF NOT EXISTS `reports` ( `id` int(11) NOT NULL AUTO_INCREMENT, `day` int(11) NOT NULL, `uid` int(11) NOT NULL, `siteid` int(11) NOT NULL, `cid` int(3) NOT NULL, `visits` int(11) NOT…
dracosu
  • 177
  • 1
  • 1
  • 8
12
votes
2 answers

Get values from RETURNING * within a transaction

When I have two INSERT SQL statements (see below) within a begin; and commit; transaction then the RETURNING * does not return anything but if I take out the begin; and commit; the RETURNING * does return the inserted record. How can I get the…
John Mitchell
  • 271
  • 4
  • 13
12
votes
3 answers

Suppress "duplicate key value violates unique constraint" errors

I'm developing a Rails 3 app that uses Postgres as its database. I've got the table shown below: Table "public.test" Column | Type | Modifiers ---------------+---------+----------- id | integer | not null …
Alex Hockey
  • 195
  • 1
  • 2
  • 6
11
votes
6 answers

Escaping single quotes in REDSHIFT SQL

I've lots of string values containing single quotes which I need to insert to a column in REDSHIFT table. I used both /' and '' to escape the single quote in INSERT statement. e.g. INSERT INTO table_Temp VALUES ('1234', 'O\'Niel'), ('3456',…
11
votes
3 answers

SQL Query to INSERT multiple rows with SELECT

I need to read data from one table and insert into multiple rows in another table in a MySQL database. Table 1 looks like: ID, name, e-mail, phone, city, ..., .... In Table 2 I need to insert data like: (row1) ID, "name", name (row2) ID,…
JErne
  • 133
  • 1
  • 1
  • 4
11
votes
2 answers

Adding column to table & adding data right away to column in PostgreSQL

I am trying to create a new column in an existing table, and using a select statement to do division to create the data I want to insert into my new column. Several of the statements I have written out will work as separate queries but I have not…
Matt P
  • 113
  • 1
  • 1
  • 4
11
votes
4 answers

Return id if a row exists, INSERT otherwise

I'm writing a function in node.js to query a PostgreSQL table. If the row exists, I want to return the id column from the row. If it doesn't exist, I want to insert it and return the id (insert into ... returning id). I've been trying variations of…
Jared
  • 2,043
  • 5
  • 33
  • 63