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
1
vote
1 answer

In Snowflake , any one used Merging into table when matched then insert into select statement

In the snowflake ,Merge into when Matched then insert () values () , this is working fine but i have a case where the insert should be from select statements Merge into when Matched then insert () select * from dummy; - but this is failing with…
1
vote
2 answers

SQL Local Variable

I use SQL Server 2005 and I have a query like this : INSERT INTO [subject] ([sch_id], [subj_from], [subj_to], ) SELECT CASE WHEN (SELECT @sched = [sch_id] FROM [schedule] WHERE…
ELM
  • 529
  • 2
  • 7
  • 19
1
vote
2 answers

SQL Insert-Select Statement

I use Visual Basic 6.0 with SQL Server 2005 Here is my code : Cn.Execute "INSERT INTO schedule (sch_name, st_id, sch_note) SELECT '" & txtSchedname.Text & "', st_id, '" & txtNote.Text & "' FROM scheduletype WHERE…
ELM
  • 529
  • 2
  • 7
  • 19
1
vote
2 answers

Remove dupilcates query in INSERT SELECT statement

I have a query which is inserting data into Clockify Table from Task Table through INSERT SELECT statements. ALTER procedure [dbo].[ClockifyAdd] AS BEGIN insert into Clockify(ClockifyId,DurationInMinutes,Date) SELECT …
1
vote
1 answer

SQL Insert into table new rows foreach field in same table

I have a database of categories, sub-categories and products. Many sub-categories and therefore their products weren't adopted by the parent categories so I'm trying to use SQL to fix this but I'm coming across some issues. Said table has three…
Haykne
  • 29
  • 5
1
vote
3 answers

Optimization needed for insert into query with select having self join

A table EMPLOYEE has below structure with 5 Million rows (10^6). Name ------ EMPNAME EMPID MANAGERID (foreign key to same table) STATUS We have a different table EmpAct where we perform insert as below INSERT INTO empact VALUES (empName,…
1
vote
1 answer

Is the order of inserts specified for INSERT IGNORE ... SELECT?

I have a table like: CREATE TABLE { email VARCHAR PRIMARY, last_login DATE } And I can populate with a select result set like: ("a@b.c", "2019-01-01"), ("a@b.c", "2019-02-01") If I will insert into that table using INSERT IGNORE ... SELECT, is…
William Entriken
  • 37,208
  • 23
  • 149
  • 195
1
vote
4 answers

INSERT INTO from multiple sources/tables

I have a table FinalTable with these columns: name, lastName, pesel, position, id_operator I want to fill my FinalTable with values from 2 other tables: AAA - this table has columns like name, lastName, pesel, position BBB - this table has columns…
Matley
  • 1,953
  • 4
  • 35
  • 73
1
vote
1 answer

Use returned value of INSERT ... RETURNING in multiple following inserts

I'm trying to use a value returned by an INSERT ... RETURNING statement in multiple following INSERTs. Say we have the following tables: CREATE TABLE hosts (host_id SERIAL, name CHARACTER VARYING(20)); CREATE TABLE interfaces (interface_id SERIAL,…
Picl
  • 149
  • 3
  • 12
1
vote
1 answer

Insert-select gets a better plan when limit clause added

This is the server i am running select version(); version --------------------------------------------------------------------------- PostgreSQL 10.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC)…
1
vote
1 answer

Optimize insert select query Oracle

Hi i need to optimize below sql query. insert into exa_table (column1, column2, column3, column4) select value1, value2, value3, value4 from (select tb2.ID, tb2.PARCELNO, tb2.SHP_ID, tb4.CUST_ID from exa_table2 tb2 join table3 tb3 on tb2.ID =…
1
vote
3 answers

LIKE operator instead of IN operator when checking tables

I am working on an SQL script to add the ID of a customer if the phone number is found inside a table. Below is a snippet of my code: CREATE TABLE #Customers ( AccountNumber nvarchar(20)) CREATE TABLE #Phones ( Number nvarchar(30)) INSERT INTO…
Jonathan
  • 15
  • 2
  • 4
1
vote
1 answer

Inserting a value from another table based on the difference of two other column values

I'm trying to add a column value to my current insert/select statement but I'm not sure if I can use a CASE WHEN clause or if I need to do this another way. Basically I have a phone number column in my main table. I need to insert a number into it…
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
1
vote
2 answers

INSERT-SELECT (Oracle PL/SQL) - Performance Issue

I have too many SELECT statements toghether with only one INSERT (maybe hundreds of them) And the system is giving a bad performance. I will explain in general words what is happening and what I'm searching for: Considering the following two…
1
vote
1 answer

MySQL Conditional INSERT

I want to do a conditional insert with MySQL. I have 2 tables (Car and CarType). The Car table got a coloumn called typeId, which points to an entry in the CarType table. I only want to insert a row in the Car table if the given typeId exists in…
Nilks
  • 446
  • 4
  • 14
1 2
3
8 9