Questions tagged [insert-select]

Use this tag for questions concerning INSERT, the SQL command which inserts data into a table using a SELECT query. Questions should also be tagged [sql] and, where applicable, tagged with the database engine in use.

This tag is used for questions dealing with the SQL INSERT command, where the data is inserted into a table using a SELECT query as shown below:

INSERT INTO <table name>
SELECT <column names> 
FROM <source table>
.....
130 questions
3
votes
2 answers

INSERT SELECT not working

Using Informix 11.7, I'm trying to execute a INSERT SELECT query with jdbc positional parameters in the select statement like this : INSERT INTO table1(id, code, label) SELECT ?, ?, ? FROM table2 WHERE ... Parameters are set like this : …
mishka
  • 155
  • 3
  • 11
3
votes
5 answers

INSERT INTO.....SELECT FROM

i want to put calory as the first value of fruits, i couldn't do it, can anyone help? $sql = 'INSERT INTO fruits VALUES('', ?, ?, ?)' SELECT calory FROM diet WHERE fruit = ? '; $this->db->query($sql,…
NestedWeb
  • 1,657
  • 2
  • 15
  • 31
2
votes
2 answers

pyodbc - insert into 'select' not working

I am trying to run INSERT INTO SELECT statement in cur.execute. The code executes without any error, but there are no records inserted in the table. Here is the table data: I execute the following query, cursor = conn.cursor() query = 'INSERT INTO…
user1584253
  • 975
  • 2
  • 18
  • 55
2
votes
1 answer

(One table) insert rows

This question is the continuation of this one. I have the following table egr: +---------+------------+ | offid | groupid | +---------+------------+ | 1 | 101 | | 1 | 202 | | 2 | 202 | | 2 | 404 …
Stephane B.
  • 542
  • 6
  • 18
2
votes
2 answers

Need an INSERT ...SELECT statement, but I also want to manually set some fields

I need an insert statement that will do roughly this: INSERT INTO tblContent(postTitle, postBody, postAuthor, postDate, postApproved, fromSite) SELECT tblSubmissions.body WHERE tblSubmissions.submissionId = 1 But I need to…
Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
2
votes
3 answers

SQL Server error: "Cannot insert explicit value for identity column" even when I SET IDENTITY_INSERT ON

I REALLY review several times, that's the reason I am asking; looking for guidance... I have one table, as the script below. Then, I set IDENTITY_INSERT ON. Then I try to do an insert select, (I NEED the very same ids) I keep getting this…
Negarrak
  • 375
  • 1
  • 4
  • 11
2
votes
1 answer

INSERT SELECT WHERE insert multiple rows from constants

I'm trying to insert multiple rows whose values are not taken from an existing table but provided from outside along with a where condition using INSERT ... SELECT ... WHERE. The following query is not working: mysql> insert into `my_table` SELECT 1…
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
2
votes
1 answer

MySQL: Copy table to another table with an extra column

I have two tables, tab1 and tab2. tab2 has all of the columns of tab1 but with an extra column for a timestamp. What I want to do is copy all of the rows from tab1 into tab2 and input the same time thae timestamp column for all rows that I insert. I…
2
votes
3 answers

MySQL: Is there a way to insert values from one table to another?

I created a table called employee CREATE TABLE employee( id INT, name VARCHAR(50), credit_card_number VARCHAR(20), expr_date CHAR(6), PRIMARY KEY(id) ) And then I have a table that stores the credit cards information CREATE…
Kara
  • 765
  • 5
  • 11
  • 29
2
votes
1 answer

combine selected columns from two tables and store it in another table in the db

Hi I want to combine selected columns from two tables and store those values into another table. example im having a table name class - classname location and another table student - studentid, name, classname Attendance - combining the two…
user3145732
  • 29
  • 1
  • 7
2
votes
3 answers

MySQL Stored Procedure with Insert Select and user variable

Problem: I have a MySQL stored procedure and I want to reset a user variable to 0 each time the procedure is called, but the variable seems to be remembering its value from each previous run and I cannot initialize it to zero. Details: I have a data…
Dan
  • 59,490
  • 13
  • 101
  • 110
2
votes
1 answer

INSERT SELECT query when one column is unique

I need to call INSERT + SELECT to import user data to different table and I need to filter user_name duplications, So I need something like this: INSERT INTO new_table SELECT email, distinct user_name, password from old_table but distinct works…
jcubic
  • 61,973
  • 54
  • 229
  • 402
1
vote
1 answer

TSQL: WITH cteTABLE insert into other_table

Possible Duplicate: Incorrect syntax near the keyword 'with'…previous statement must be terminated with a semicolon I want to select hierachical data and insert it in a table. Therefore i need to use the WITH statment in my insert. This works…
jzzh
  • 23
  • 7
1
vote
2 answers

psql 15: insert ... select triggering autoincrement where NULL values

I'm trying to "copy/paste" rows of a table with bigserial id column, without listing all columns names in the query. There is a related post https://stackoverflow.com/a/15344327, which does not work with psql. The isses is that psql does not trigger…
aAWnSD
  • 124
  • 10
1
vote
0 answers

Oracle insert into select for update

Let's say I've got two tables t1 and t2, and I'm inserting data into t1 using columns selected from t2. create or replace package body for_update_test as procedure test(out_response out varchar2) as v_id number; begin select id into…
mystarrocks
  • 4,040
  • 2
  • 36
  • 61
1
2
3
8 9