Questions tagged [select-into]

The SQL command SELECT INTO statement copies data from one table into a new table. See https://www.w3schools.com/sql/sql_select_into.asp

SELECT INTO is available in various database systems so please tag the type of database being used as well.

158 questions
78
votes
3 answers

Select multiple columns into multiple variables

How can I do in one select with multiple columns and put each column in a variable? Something like this: --code here V_DATE1 T1.DATE1%TYPE; V_DATE2 T1.DATE2%TYPE; V_DATE3 T1.DATE3%TYPE; SELECT T1.DATE1 INTO V_DATE1, T1.DATE2 INTO V_DATE2, T1.DATE3…
Hélder Gonçalves
  • 3,822
  • 13
  • 38
  • 63
58
votes
11 answers

SELECT INTO and "Undeclared variable" error

When I try to execute following query: SELECT id_subscriber INTO newsletter_to_send FROM subscribers I get an error: #1327 - Undeclared variable: newsletter_to_send What is wrong with that query ?
hsz
  • 148,279
  • 62
  • 259
  • 315
41
votes
10 answers

Preserving ORDER BY in SELECT INTO

I have a T-SQL query that takes data from one table and copies it into a new table but only rows meeting a certain condition: SELECT VibeFGEvents.* INTO VibeFGEventsAfterStudyStart FROM VibeFGEvents LEFT OUTER JOIN VibeFGEventsStudyStart ON …
dumbledad
  • 16,305
  • 23
  • 120
  • 273
32
votes
6 answers

SQL Server 'select * into' versus 'insert into ..select *

Say table1 and table2 already exist, is there any difference between these queries query1 :- select * into table1 from table2 where 1=1 query2: - insert into table1 select * from table2
Sujit Prabhakaran
  • 295
  • 3
  • 12
  • 26
26
votes
1 answer

Is it possible to create indexes on a temp table when using SELECT INTO?

I am loading data from a CSV file into a temp staging table and this temp table is being queried a lot. I looked at my execution plan and saw that a lot of the time is spent scanning the temp table. Is there any way to create index on this table…
Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
25
votes
1 answer

Declare row type variable in PL/pgSQL

As I found SELECT * FROM t INTO my_data; works only if: DO $$ DECLARE my_data t%ROWTYPE; BEGIN SELECT * FROM t INTO my_data WHERE id = ?; END $$; Am I right? If I want to get only 2-3 columns instead of all columns. How can I define my_data? That…
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
20
votes
2 answers

SQL SELECT INTO query with a Join, Error "There is already an object named '*****' in the database."

I'm currently trying to copy data from table into another by using a SELECT INTO query. But Im receiving an error in SQL Server. "Msg 2714, Level 16, State 6, Line 2 There is already an object named 'Product' in the database." Im still very new to…
James O Brien
  • 257
  • 1
  • 3
  • 10
14
votes
2 answers

SELECT or PERFORM in a PL/pgSQL function

I have this function in my database: CREATE OR REPLACE FUNCTION "insertarNuevoArticulo"(nombrearticulo character varying, descripcion text, idtipo integer, idfamilia bigint, artstock integer, minstock integer, maxstock integer, idmarca bigint,…
dbncourt
  • 560
  • 4
  • 10
  • 27
11
votes
3 answers

Using temp table in PL/pgSQL procedure for cleaning tables

I'm trying to delete all data related to a user id from a game database. There is a table holding all games (each played by 3 players): # select * from pref_games where gid=321; gid | rounds | …
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
11
votes
2 answers

How to select the value of a variable in Oracle?

I'm new to Oracle. How can I set this variable and show its value? declare nextId number; begin select HIBERNATE_SEQUENCE.nextval into nextId from dual; select nextId from dual; end; It complains that an INTO clause is…
The Light
  • 26,341
  • 62
  • 176
  • 258
10
votes
2 answers

How to store selection result in to variable in Oracle procedure

I write a simple procedure. I try to store selection result in variable. I use "SELECT INTO" query but I can not doing this. Example: DECLARE v_employeeRecord employee%ROWTYPE; BEGIN SELECT * INTO v_employeeRecord FROM Employee WHERE…
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
10
votes
2 answers

There is already an object named 'tbltable1' in the database

I am trying to insert data from one table to another with same structure, select * into tbltable1 from tbltable1_Link I am getting the following error message: There is already an object named 'tbltable1' in the database.
Jaison
9
votes
3 answers

How to define the type (int) for a new field in SQL SELECT INTO statement in MS Access

I want to define the new field in a select into statement as integer. However, the NewField ends up as binary. How can I accomplish this? SELECT ExistingField1, ExistingField2, NewField INTO NewTable FROM ExistingTable I searched a lot but couldn't…
Onur T
  • 93
  • 1
  • 1
  • 4
9
votes
3 answers

How to use SELECT... INTO with a JOIN?

I have the following example code DECLARE myRow table%rowtype myVar table2.column%type BEGIN SELECT table.col1, table.col3, table.col4, table2.column INTO myRow FROM table JOIN table2 On table.col6 = table2.col1; …
BLaZuRE
  • 2,356
  • 2
  • 26
  • 43
8
votes
1 answer

add extra columns in a SELECT INTO statement

I am doing a SELECT INTO statement to create a new table from some values in another table. I want to add two extra columns onto the newly created table (pol_eff_dt, pol_exp_dt) and make them all NULL initially (these columns also exist in the…
intA
  • 2,513
  • 12
  • 41
  • 66
1
2 3
10 11