Temporary tables in hana - database

it it possible to write script in hana that crate temporary table that is based
on existing table (with no need to define columns and columns types hard coded ):
create local temporary table #mytemp (id integer, name varchar(20));
create temporary table with the same columns definitions and contain the
same data ? if so ..i ill be glad to get some examples
i am searching the internet for 2 days and i couldn't find anything useful
thanks

Creating local temporary tables based on dynamic structure definition is not supported in SQLScript.
The question would be: for what do you want to use it?
Instead of a local temp. table you can use a table variable in most cases.

By querying sys.table_columns view, you can get the list and properties of source table and build a dynamic CREATE script then Execute to create the table.
You can find SQL codes for a sample case at Create Table Dynamically on HANA Database
For table columns read
select * from sys.table_columns where table_name = 'TABLENAME';

Seems to work in the hana version I have. I'm not sure how to find out what the version.
PROCEDURE "xxx.yyy.zzz::MY_TEST"(
OUT "OUT_COL" NVARCHAR(200)
)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
AS
BEGIN
create LOCAL TEMPORARY TABLE #LOCALTEMPTABLE
as
(
SELECT distinct 'Cola' as out_col
FROM "SYNONYMS1"
);
select * from #LOCALTEMPTABLE ;
DROP TABLE #LOCALTEMPTABLE;
END

The newer HANA version (HANA 2 SPS 04 Patch 5 ( Build 4.4.17 )) supports your request:
create local temporary table #tempTableName' like "tableTypeName";

This should inherit the data types and all exact values from whatever query is in the parenthesis:
CREATE LOCAL COLUMN TEMPORARY TABLE #mytemp AS (
SELECT
"COLUMN1",
"COLUMN2",
"COLUMN3"
FROM MyTable
);
-- Now you can add the rest of your query here as such:
SELECT * FROM #mytemp

I suppose you can just write :
create column table #MyTempTable as ( select * from MySourceTable);
BR,

Related

In citusDB, how do I find out how my data is distributed?

In CitusDB, I can create an empty table with:
CREATE TABLE table1 (col1 text, col2 text);
I can tell table1 how to partition the data, which will later be loaded into the table, by running this:
SELECT create_distributed_table('table1', 'col1');
In this moment, I will then know how my table is distributed across CitusDB nodes.
However, if I come across a new table that I didn't create, but I know it is distributed, how do I know what column the table is distributed on?
You want to use the citusDB column_to_column function described in the citus db docs: http://docs.citusdata.com/en/v9.3/develop/api_udf.html
SELECT column_to_column_name(logicalrelid, partkey) AS dist_col_name
FROM pg_dist_partition
WHERE logicalrelid='<table>'::regclass;

Changed column name to upper case when create a new table through script in Oracle how to resolve this?

I am using Oracle tool (Oracle SQL Developer - Version 19.2.1.247).
When i create new table in Oracle Db then it change all column name in uppercase i.e.(CUSTOMERID), but i want to keep column name i.e.(CustomerId).I am looking for solutions how to resolve this.
I did some try to change formatting of editor as well code setting in Tools -> Preference but not found any proper things.
Thanks in advance.
You should avoid doing that. Every object ( table, column, index, sequence, trigger... ) is stored in uppercase in the Oracle dictionary.
However, if you want to store the name in lowercase, you must use double quotation
SQL> create table test ( c1 number );
Table created
SQL> select column_name from all_tab_columns where table_name = 'TEST';
COLUMN_NAME
C1
SQL> create table test ( "c1" number );
Table created
SQL> select column_name from all_tab_columns where table_name = 'TEST';
COLUMN_NAME
c1
Keep in mind that if you store the value in lowercase, any search or program that uses the dictionary will have to take this in consideration. That is why I believe it is not a good practice.
Oracle has a default functionality where it will convert all unquoted table/column identifiers to upper case, therefore add double quotes around the names should resolve your issue.

Get recently created schema in PostgreSQL

I have more than 150 schemas in PostgreSQL, every time when I perform an action a new schema will be created with some random name with numbers. It is hard to find which is the new schema created.
I use \dn to list schemas in PostgreSQL, but it doesn't display schemas in created order. How do I list either recently created schema or schemas sorted by creation date?
Any schema has oid column - numeric unique identifier (that is increased only). So you can use ORDER BY oid DESC
SELECT * FROM pg_namespace ORDER BY oid DESC LIMIT 10;
Pavel's answer is good and may get you everything you need, but if you want more flexibility, I would recommend using event triggers:
CREATE TABLE schema_creation_history
(schema regnamespace primary key,
created timestamp with time zone not null default now()
);
CREATE FUNCTION log_schema_create() RETURNS event_trigger
LANGUAGE plpgsql AS
$$
BEGIN
INSERT INTO schema_creation_history (schema) SELECT objid::regnamespace FROM pg_event_trigger_ddl_commands();
END;
$$
;
CREATE EVENT TRIGGER schema_create_trigger
ON ddl_command_end
WHEN TAG IN ('CREATE SCHEMA')
EXECUTE FUNCTION log_schema_create();
# create schema test;
CREATE SCHEMA
# select * from schema_creation_history ;
schema | created
--------+------------------------------
test | 2019-09-11 12:32:21.16346+00
(1 row)
You could add another trigger for DROP SCHEMA to make sure the table is cleaned up.

How can I transfer data from one table to another, overwriting old data?

I need some help for transfering data from one table to another.
As you can see there are 2 databases.
I would like to transfer the table datas "PinterSet" located in the database Contrinex.GPO in the table "PrinterSet" located in the database Contrinex.GPOQA.
There are already datas in the table "PrinterSet" of Contrinex.GPOQA but I would overwrite and put the datas from "PrinterSet" of Contrinex.GPO.
So how can I do that ?
here is your code..
truncate table Contrinex.GPOQA.dbo.PrinterSet
go
insert into Contrinex.GPOQA.dbo.PrinterSet
select * from Contrinex.GPO.dbo.PrinterSet
TRUNCATE TABLE [Contrinex.GPOQA].dbo.PinterSet
GO
INSERT INTO [Contrinex.GPOQA].dbo.PinterSet (...)
SELECT ...
FROM [Contrinex.GPO].dbo.PinterSet
select data from first database table and insert it into the second database table as
INSERT INTO GPOQA.PrinterSet SELECT * from GPO.PrinterSet
if want some perticular columns then set column names as
INSERT INTO GPOQA.PrinterSet a SET a.column1=b.column1,.... SELECT column1,... from GPO.PrinterSet b
You can use Sql Server Export functianality, where you can transfer data from one table to another across Database.
Please refer the below link on using the SQL Server Export
http://searchsqlserver.techtarget.com/feature/The-SQL-Server-Import-and-Export-Wizard-how-to-guide

how to export records/data from one database table to another database table?

How do you export records from one database table and import it into another database table?
(same table structure).
If the table have the exact same structure, and no autogenerated fields you can use:
insert into DestinationTable
select * from SourceTable
You can also use the
select *
into DestinationTable
from SourceTable
syntax, to create and fill the destination table on the fly.
If you also want to keep you identity colums same you can easily do it using code smith template. Download the templates from here and use ScriptTableData.cst template in them. Before that you will required to install code smith on you machine too.

Resources