Questions tagged [insert-into]

The "INSERT INTO" is a SQL statement that is used to insert new rows into a table.

715 questions
119
votes
5 answers

INSERT INTO...SELECT for all MySQL columns

I'm trying to move old data from: this_table >> this_table_archive copying all columns over. I've tried this, but it doesn't work: INSERT INTO this_table_archive (*) VALUES (SELECT * FROM this_table WHERE entry_date < '2011-01-01…
Kyle
  • 17,480
  • 23
  • 68
  • 100
109
votes
5 answers

mysql -> insert into tbl (select from another table) and some default values

As the title says, I am trying to insert into one table selecting values from another table and some default values. INSERT INTO def (catid, title, page, publish) (SELECT catid, title from abc),'page','yes') INSERT INTO def (catid, title, page,…
wannabeprogrammer
  • 1,105
  • 2
  • 8
  • 8
83
votes
11 answers

Adding a line break in MySQL INSERT INTO text

Could someone tell me how to add a new line in a text that I enter in a MySql table? I tried using the '\n' in the line I entered with INSERT INTO statement but '\n' is shown as it is. Actually I have created a table in MS Access with some data. MS…
Muhammed Umer
  • 881
  • 1
  • 6
  • 6
67
votes
7 answers

MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows?

MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows? INSERT INTO Results ( People, names, ) VALUES ( ( SELECT d.id FROM Names f JOIN People d ON d.id = f.id …
stackoverflow
  • 15,804
  • 43
  • 121
  • 181
32
votes
4 answers

sql - insert into multiple tables in one query

assuming that i have two tables, names and phones and i want to insert data from some input to the tables, in one query- How can it be done? Please, if it can be done, explain the syntax.
yossi
  • 2,814
  • 6
  • 37
  • 58
21
votes
3 answers

Does INSERT INTO ... SELECT ... always match fields by ordinal position?

My tests seem to confirm that INSERT INTO a (x, y) SELECT y, x FROM b maps b.y to a.x, i.e., the fields are matched only by ordinal position and not by name. Is this always the case, i.e., can I rely on that behaviour? Unfortunately, the…
Heinzi
  • 151,145
  • 51
  • 326
  • 481
17
votes
5 answers

Move row from one table to another?

I have two tables with the same column definitions. I need to move (not copy) a row from one table to another. Before I go off and use INSERT INTO/DELETE (in a transaction), is there a smarter way? SQL Server 2005
lance
  • 15,310
  • 17
  • 70
  • 129
15
votes
9 answers

SQL Insert Into Temp Table in both If and Else Blocks

I'm trying to populate a temp table based on the result of a condition in SQL 2005. The temp table will have the same structure either way, but will be populated using a different query depending on the condition. The simplified example script…
CuppM
  • 1,599
  • 4
  • 17
  • 30
15
votes
2 answers

Rails 3.2 + MySQL: Error: Field 'created_at' doesn't have a default value: INSERT INTO

I created a new migration, where is mentioned ... t.timestamps in the created table are added these two columns ... | created_at | datetime | NO (Null) | | NULL (Default) | | updated_at | datetime | NO (Null) | |…
user984621
  • 41,002
  • 66
  • 200
  • 371
14
votes
3 answers

SELECT INTO syntax for Snowflake Datawarehouse

I believe there's an SELECT INTO-like syntax in Snowflake, but I am unable to find documentation or examples to use it. CREATE TABLE raw_data ( Timestamp TIMESTAMP NOT NULL, Date DATE NOT NULL, UserID STRING, Address STRING, …
Kirk Broadhurst
  • 25,044
  • 13
  • 91
  • 149
13
votes
4 answers

Inserting records into a MySQL table using Java

I created a database with one table in MySQL: CREATE DATABASE iac_enrollment_system; USE iac_enrollment_system; CREATE TABLE course( course_code CHAR(7), course_desc VARCHAR(255) NOT NULL, course_chair VARCHAR(255), PRIMARY…
user2539182
12
votes
1 answer

Using WITH clause with INSERT statement in POSTGRESQL

I have a requirement in which I need to get one column from another table and insert that column data with some other data into another table. Example: If the cust_id='11' then I need to get the cust_code from cust table (let's say it returns…
Neeraj Wadhwa
  • 525
  • 2
  • 6
  • 18
12
votes
3 answers

INSERT INTO using a query, and add a default value

I want run an INSERT INTO table SELECT... FROM... The problem is that the table that I am inserting to has 5 columns, whereas the table I am selecting from has only 4. The 5th column needs to be set do a default value that I specify. How can I…
DanGordon
  • 571
  • 3
  • 6
  • 23
12
votes
3 answers

Inserting values into tables Oracle SQL

I'm trying to insert values into an 'Employee' table in Oracle SQL. I have a question regarding inputting values determined by a foreign key: My employees have 3 attributes that are determined by foreign keys: State, Position, & Manager. I am using…
adohertyd
  • 2,639
  • 17
  • 48
  • 77
11
votes
3 answers

Why is MySQL 'insert into ... select ...' so much slower than a select alone?

I'm trying to store a query result in a temporary table for further processing. create temporary table tmpTest ( a FLOAT, b FLOAT, c FLOAT ) engine = memory; insert into tmpTest ( select a,b,c from someTable where ... ); But…
Ben
  • 4,037
  • 5
  • 29
  • 46
1
2 3
47 48