How to create a temporary table without referring to permanent table i.e. without using select statement - sql

Is there any way to create our own temporary table or using 'With' clause to create a table with our own columns and values without using any select statement from another permanent table in a query. That temporary table I want to refer in my main query.
Please help me with this.
Thanks.

You can certainly use a WITH however depending on the data you might have to use a lot of UNION ALL statements unless your requirement can allow to use some kind of recursive query. The below is for SQL-Server.
WITH temp_table(id, name) AS
(
SELECT 1, 'abc'
UNION ALL
SELECT 2, 'cde'
)
SELECT * FROM temp_table
You can read this for more details - https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-ver15

Related

BigQuery: Run queries to create table and append to the table if table exist

Is it possible to run query to create a table if it does not exist, and append to the table if the table already exists? I like to write a single query to create or append. Note: I am using Admin console for now, will be using API eventually.
I have following query:
CREATE TABLE IF NOT EXISTS `project_1.dataset_1.tabe_1`
OPTIONS(
description="Some desc"
) AS
SELECT *
FROM source_table
I get following error:
A table named project_1.dataset_1.tabe_1 already exists.
Above query creates a table named 'table_1' if it does not exist under 'project_1.dataset_1', and append to the table if the table already exists.
IF
(
SELECT
COUNT(1)
FROM
`project_1.dataset_1.__TABLES_SUMMARY__`
WHERE
table_id='table_1') = 0
THEN
CREATE OR REPLACE TABLE
`project_1.dataset_1.table_1` AS
SELECT
'apple' AS item,
'fruit' AS category
UNION ALL
SELECT
'leek',
'vegetable';
ELSE
INSERT
`project_1.dataset_1.table_1` ( item,
category )
SELECT
'lettuce' AS item,
'vegetable' AS category
UNION ALL
SELECT
'orange',
'fruit';
END IF;
This seems like it may be a good opportunity to leverage scripting within a single query to accomplish your needs.
See this page for adding control flow to a query to handle an error (e.g. if the table create fails due to existing). For the exception case, you could then INSERT ... SELECT statement as needed.
You can do this via the API as well if you prefer. Simply issue a tables.get equivalent appropriate to the particular library/language you choose and see if the table exists, and then insert the appropriate query based on that outcome of that check.

SELECT INTO query to create MEMORY_OPTIMIZED table

is it possible to generate a MEMORY_OPTIMIZED table with an SELECT INTO statement?
Something like:
SELECT *
INTO MemoryOptimizedTargetTable
FROM SourceTable;
Or at least convert the target after the SELECT INTO with an ALTER statement?
Thanks
No.
With SQL Server 2017 it will be possible to specify a filegroup, but that's as far as it goes into customizing the created table.
Since SELECT ... INTO ... is equivalent to CREATE TABLE, to be fully customizable it would require all options CREATE TABLE has. This is not really feasible. Once you realizE that in effect you are creating a table and inserting rows into it, why not do just that?
CREATE TABLE ...
INSERT ... SELECT ...

Temporary tables and AS statement usage

If I have a query, say:
SELECT *
FROM ( SELECT fields
FROM tables
WHERE condition
)
AS TEMP_TABLE
are the results of the above query saved in a temporary table called TEMP_TABLE so as I can perform another query on it later? Will the query below be executed successfully when using DB2?
SELECT fields
FROM TEMP_TABLE
WHERE condition
The answer is NO, it's just an alias for a subquery.
If you want to use it later, you have to explicitly create it.
you can create temporary table in following way.
CREATE TEMPORARY TABLE temp_table AS (
SELECT fields
FROM tables
WHERE condition
);
then you can retrieve data from the temp table as below.
SELECT * FROM temp_table

Is there an automated way to create a temp table in SQL Server

I have a rather complex SELECT statement in a stored procedure that I am updating to insert the rows from the select into a temp table. To define the temp table, I need to know the data type of each every item selected.
Is there a easy way (a script maybe) that I can use to determine the data types and the temp table structure instead of going to each table's definition in the select to find out what it is?
PS: I can't use a Common table expression as I need to use this temp table several times within the proc
SELECT
blah
INTO
#temp
FROM
wibble
blah and wibble are not secret syntax. Please replace these with your own SQL :)
SELECT * INTO #temp FROM TABLE1
All columns in TABLE 1 gets into your temp table now

"select * into table" Will it work for inserting data into existing table

I am trying to insert data from one of my existing table into another existing table.
Is it possible to insert data into any existing table using select * into query.
I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union to insert all records into same table
eg.
select * into #tblExisting from tblExisting
drop table tblExisting
select * into tblExisting from #tblExisting union tblActualData
Here tblExisting is the table where I actually want to store all data
tblActualData is the table from where data is to be appended to tblExisting.
Is it right method.
Do we have some other alternative ?
You should try
INSERT INTO ExistingTable (Columns,..)
SELECT Columns,...
FROM OtherTable
Have a look at INSERT
and SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE
No, you cannot use SELECT INTO to insert data into an existing table.
The documentation makes this very clear:
SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.
You generally want to avoid using SELECT INTO in production because it gives you very little control over how the table is created, and can lead to all sorts of nasty locking and other performance problems. You should create schemas explicitly and use INSERT - even for temporary tables.
#Ryan Chase
Can you do this by selecting all columns using *?
Yes!
INSERT INTO yourtable2
SELECT * FROM yourtable1
Update from CTE? http://www.sqlservercentral.com/Forums/Topic629743-338-1.aspx