How to create Temporary table with AS in postgresql? - sql

Create Temporary Table Temp3 (
user_id int not null,
) AS query(
select id
from users
)
I use the above code in postgresql but it says:
ERROR: syntax error at or near ")"
LINE 3: ) AS (
^
****** Error ******
ERROR: syntax error at or near ")"
SQL state: 42601
Character: 55
How can I fix it to do the same job? Thx ahead!

It is much simpler than your try:
create temporary table Temp3 as
select id as user_id
from users;
SeeCREATE TABLE AS

Related

Add generated column to an existing table Postgres

I am trying to add a generated column to an existing table with this script.
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
but I am getting an error:
SQL Error [42601]: ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
What is the issue? I don't get it.
In the schema of my Asset_Store table the column
OR_ID is int and Asset_ID is varchar(100).
I guess it expects a slightly different syntax... but what is the right syntax?
Your syntax is correct. Your version of PostgreSQL apparently is not.
In version 12:
create table asset_store(or_id text, asset_id text);
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
ALTER TABLE
Time: 17.678 ms
More general, simplified command
ALTER TABLE "items"
ADD COLUMN "revenue" numeric
GENERATED ALWAYS AS ("price" * (1-"discount")) STORED;

How to save query result when using SQL in R?

If just SQL, we can use the following code to save a query result in a temporary table, so that we can use the result later.
CREATE TABLE #TEMPTABLE
(
Column1 type1,
Column2 type2,
Column3 type3
)
INSERT INTO #TEMPTABLE
SELECT ...
SELECT *
FROM #TEMPTABLE ...
This answer is from How to save select query results within temporary table?
However, I am using R to connect HANA. I need to use SQL query in R to select data from HANA. I need a tempory table for my query result. My code is like this:
sqlQuery(ch,paste('
CREATE TABLE #myTemp(
"/BIC/ZSALE_OFF" INT
)
INSERT INTO #myTemp
SELECT
"/BIC/ZSALE_OFF"
FROM
"SAPB1D"."/BIC/AZ_RT_A212"
'))
I have got the following error information:
[1] "42000 257 [SAP AG][LIBODBCHDB DLL][HDBODBC] Syntax error or access violation;257 sql syntax error: incorrect syntax near \"INSERT\": line 5 col 19 (at pos 124)"
[2] "[RODBC] ERROR: Could not SQLExecDirect '\n CREATE TABLE #myTemp(\n \"/BIC/ZSALE_OFF\" INT\n )\n INSERT INTO #myTemp\n SELECT\n \"/BIC/ZSALE_OFF\"\n FROM\n \"SAPB1D\".\"/BIC/AZ_RT_A212\"\n
Without the temperory result part, the code for just query is correct:
sqlQuery(ch,paste('
SELECT
"/BIC/ZSALE_OFF"
FROM
"SAPB1D"."/BIC/AZ_RT_A212"
'))
I am not sure if the grammar is correct, or there is something else I do not understand.

ERROR: syntax error at or near "SELECT"

I am really new to postgres. The question looks very simple but I just cant see where I got wrong.
I a table created as follows:
CREATE TABLE IF NOT EXISTS t(
tn VARCHAR(30) NOT NULL,
PRIMARY KEY(tn)
);
I want to insert an instance if the instance does not exist. Here is my code:
INSERT INTO t (tn)
VALUES
(SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q')) ;
And the psql console keeps giving me the error
ERROR: syntax error at or near "SELECT"
I have checked every piece of code individually, for instance both
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');
and
INSERT INTO t (tn) VALUES ('p');
run without error. But error occurs when I put them together.
Does anyone know where I got wrong..?
Lose VALUES and the brackets...
INSERT INTO t (tn)
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');

How to retrieve data from Table-Function in DB2

I have a Table Function it return a table of (student_id,student_name)
I want to call it and insert the result into another table
I use
INSERT INTO STUDENT_TMP SELECT Table(MyDB.fn_getStudent())
but i did not get the result
I have got an error :
ERROR: DB2 SQL Error: SQLCODE=-390, SQLSTATE=42887,
SQLERRMC=MyDB.AA;SQL131208155041300,DRIVER=3.67.26
Error Code: -390
I found the followin exsample on ibm sites:
select t1.timeid, t1.storeid, t1.sales
from time, store, table (cvsample.salesfunc(time.timeid, store.storeid)) as t1
where time.timeid = t1.timeid and store.storeid = t1.storeid;
notice the syntax: table (cvsample.salesfunc(time.timeid, store.storeid)) as t1
so you prob dont need fields and 'as' you still need '*' and the 'FROM'
so
INSERT INTO STUDENT_TMP SELECT * FROM Table (MyDB.fn_getStudent())

Oracle copy data to another table

In the Oracle, I copy data from a backup to a new table, it doesn't work.
what is the correct syntax ?
Thanks
select CODE, MESSAGE into EXCEPTION_CODES (CODE, MESSAGE)
from Exception_code_tmp
the error is
**SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"**
You need an INSERT ... SELECT
INSERT INTO exception_codes( code, message )
SELECT code, message
FROM exception_code_tmp
If you want to create table with data . First create the table :
create table new_table as ( select * from old_table);
and then insert
insert into new_table ( select * from old_table);
If you want to create table without data . You can use :
create table new_table as ( select * from old_table where 1=0);
Creating a table and copying the data in a single command:
create table T_NEW as
select * from T;
* This will not copy PKs, FKs, Triggers, etc.
insert into EXCEPTION_CODES (CODE, MESSAGE)
select CODE, MESSAGE from Exception_code_tmp
create table xyz_new as select * from xyz where 1=0;
http://www.codeassists.com/questions/oracle/copy-table-data-to-new-table-in-oracle