-1

My SQL query returns 132 rows with 1 column, I want to store them for a later use.

I have been through many posts on StackOverflow. I am trying following but it wont help:

DECLARE @store
SET @store=(select paramkey from parameter.p1val2 where paramname = 'BANKHOLIDAYS' and paramkey LIKE 'FI%')

Is it so I have to use a temp table to store all rows ? so I tried following

CREATE TABLE temp_table select paramkey from parameter.p1val2 where paramname = 'BANKHOLIDAYS' and paramkey LIKE 'FI%'

IT gives following error

An unexpected token "CREATE TABLE temp_table" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.62.57 SQL Code: -104, SQL State: 42601

Br Sara

Igor
  • 60,821
  • 10
  • 100
  • 175
Sara
  • 181
  • 1
  • 3
  • 16
  • 1
    Are you using MySQL or MS SQL Server? – jarlh Aug 30 '18 at 12:50
  • 1
    Based on the error code I am assuming this is IBM's [tag:db2]. [edit] the question tags accordingly if that is not correct. – Igor Aug 30 '18 at 12:57
  • 1
    Possible duplicate of [db2 equivalent of tsql temp table](https://stackoverflow.com/questions/11422572/db2-equivalent-of-tsql-temp-table) – Eric Brandt Aug 30 '18 at 12:59

5 Answers5

1

I was using db2 and following thing actually worked for me, thankyou everyone

CREATE TABLE temp_table (paramkey varchar(200))
insert into temp_table 

select substr(paramkey,3) from parameter.p1val2 where paramname = 'BANKHOLIDAYS' and paramkey LIKE 'FI%'
Sara
  • 181
  • 1
  • 3
  • 16
0

Just use it like :

select paramkey INTO #temp_table 
from parameter.p1val2 where paramname = 'BANKHOLIDAYS' and paramkey LIKE 'FI%'

SELECT * FROM #temp_table

Use #temp_table later now.

Praneet Nadkar
  • 823
  • 7
  • 16
0

For MySQL, it would be

CREATE TEMPORARY TABLE foo SELECT paramkey FROM ...
-- Now use it ...
DROP TEMPORARY TABLE foo;
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
0

for mssql server temp table creation

  SELECT  * INTO ##tempTable FROM (select paramkey from parameter.p1val2
            where paramname = 'BANKHOLIDAYS' and paramkey   LIKE 'FI%')

for mysql temp table creation

    CREATE TEMPORARY TABLE temp_table
   (select paramkey from parameter.p1val2
    where paramname = 'BANKHOLIDAYS' and paramkey   LIKE 'FI%');
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

In Oracle, you can use

CREATE TABLE tempTable as SELECT * FROM originTable WHERE x=y;

DB2 has something similar as well

CREATE TABLE tempTABLE AS (
    SELECT *
    FROM originTABLE WHERE x=y
) WITH DATA

Fairly sure other DBMS have that too with some syntax variations.

Dessma
  • 599
  • 3
  • 11