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.

138 questions
57
votes
2 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,092
  • 12
  • 33
  • 63
50
votes
10 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
  • 136,835
  • 55
  • 236
  • 297
32
votes
9 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
  • 12,928
  • 20
  • 97
  • 226
31
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
  • 769
  • 3
  • 12
  • 23
23
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
  • 23,112
  • 16
  • 96
  • 174
20
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,122
  • 9
  • 40
  • 71
16
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
  • 217
  • 1
  • 3
  • 10
13
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
  • 450
  • 3
  • 9
  • 24
10
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
  • 24,407
  • 61
  • 164
  • 254
9
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
  • 18,345
  • 68
  • 208
  • 375
9
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
  • 31,576
  • 17
  • 81
  • 124
9
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
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,131
  • 10
  • 34
  • 50
7
votes
5 answers

Unable to 'SELECT INTO' when value doesn't exist

SELECT Value1 INTO lValue FROM Table1 WHERE Field1 = lTempValue; This works fine when the match is true. But if the match isn't true, I receive an error. ORA-01403: no data found Ideally, that's fine with me because I'm going to check that…
XstreamINsanity
  • 3,748
  • 9
  • 42
  • 59
6
votes
2 answers

Amazon Aurora PostgreSQL SELECT INTO OUTFILE S3

We are trying to export data from an Amazon Aurora PostgreSQL database to an S3 buckets. The code being used is like this: SELECT * FROM analytics.my_test INTO OUTFILE S3 's3-us-east-2://myurl/sampledata' FIELDS TERMINATED BY ',' LINES…
1
2 3
9 10