How to save query result when using SQL in R? - sql

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.

Related

Creating a table from a prior WITH clause in BigQuery

WITH LAYER AS (
SELECT
SPLIT(de_nest, '|')[OFFSET(1)] AS product,
....
FROM `table`,
UNNEST(SPLIT(LOWER(REPLACE(variable, '^', '-')), '-')) AS de_nest
)
-- Filter out empty products
CREATE OR REPLACE TABLE `newtable` AS
SELECT * FROM LAYER WHERE product is NOT NULL
This leads me to the following error.
Syntax error: Expected "(" or "," or keyword SELECT but got keyword CREATE at [25:1]
But I cannot seem to find a sensible way of resolving this. My first workload is doing the un-nesting of the first table and the second is doing some filtering on those columns generated from the un-nesting process.
You should try to put the CTE declaration after the CREATE statement:
CREATE OR REPLACE TABLE `new_table` AS
WITH layer AS ...
EDIT: a complete example
CREATE OR REPLACE TABLE
`your_project.your_dataset.your_table` AS
WITH
layer1 AS (
SELECT
'this is my CTE' AS txt),
another_cte AS (
SELECT
txt,
SPLIT(txt, ' ') AS my_array
FROM
layer1)
SELECT
*
FROM
another_cte
Creates the following table

How to insert table in Databricks using magic SQL operator

I have create the following SQL table in databricks (using the magic %sql) as follows:
%sql
CREATE TABLE mytable (
id INT
,name STRING
,met_area_name STRING
,state STRING
,type STRING
) USING CSV
I am now trying insert data into the table using the following command:
%sql
INSERT INTO TABLE mytable VALUES (id,name,type)
SELECT DISTINCT criteria1, criteria2, 'b'
FROM tablex
WHERE somecriteria1 = 0
ORDER BY somecriteria2;
However, I'm getting the following error:
Error in SQL statement: ParseException:
mismatched input 'FROM' expecting <EOF>(line 2, pos 2)
== SQL ==
INSERT INTO TABLE mytable VALUES (id,name,type)
FROM tablex
--^^^
WHERE somecriteria1 = 0
ORDER BY somecriteria2
I'm sure there is something very obvious that I'm missing, but I can't see it.
Any assistance much appreciated.
Cheers

USING CAST STATEMENT IN INSERT INTO STATEMENT SQL

Below (1) is the query which I wrote and when I am not using CAST i am getting error (2) and when am using cast its throwing another syntax error(3)
(1)
/*DROP TABLE IF EXISTS LFB1_BACKUPTABLE
CREATE TABLE LFB1_BACKUPTABLE AS
(SELECT * FROM LFB1);
*/
INSERT INTO LFB1_BACKUPTABLE (
MANDT,LIFNR,BUKRS,PERNR,ERDAT,ERNAM,SPERR,LOEVM,ZUAWA,
AKONT,BEGRU,VZSKZ,ZWELS,XVERR,ZAHLS,ZTERM,EIKTO,ZSABE,
KVERM,FDGRV,BUSAB,LNRZE,LNRZB,ZINDT,ZINRT,DATLZ,XDEZV,
WEBTR,KULTG,REPRF,TOGRU,HBKID,XPORE,QSZNR,QSZDT,QSSKZ,
BLNKZ,MINDK,ALTKN,ZGRUP,MGRUP,UZAWE,QSREC,QSBGR,QLAND,
XEDIP,FRGRP,TOGRR,TLFXS,INTAD,XLFZB,GUZTE,GRICD,GRIDT,
XAUSZ,CERDT,CONFS,UPDAT,UPTIM,NODEL,TLFNS,AVSND,AD_HASH,
J_SC_SUBCONTYPE,J_SC_COMPDATE,J_SC_OFFSM,J_SC_OFFSR,
BASIS_PNT,GMVKZK,PREPAY_RELEVANT,ASSIGN_TEST, CAST(_CELONIS_CHANGE_DATE AS DATE) AS _CELONIS_CHANGE_DATE
)
SELECT DISTINCT * FROM LFB1
WHERE MANDT||LIFNR||BUKRS NOT IN (SELECT DISTINCT MANDT||LIFNR||BUKRS FROM
LFB1_BACKUPTABLE);
(2) Execution error:
[Vertica]VJDBC ERROR: Column "_CELONIS_CHANGE_DATE" is of type timestamptz but expression is of type varchar
(3) Execution error:
[Vertica]VJDBC ERROR: Syntax error at or near "CAST"
Why are you using a cast statement in your insert table column list? You can't change the type of the destination table columns.
You need to change the type of the values you are inserting.
INSERT INTO LFB1_BACKUPTABLE (
MANDT,LIFNR,BUKRS,PERNR,ERDAT,ERNAM,SPERR,LOEVM,ZUAWA,
AKONT,BEGRU,VZSKZ,ZWELS,XVERR,ZAHLS,ZTERM,EIKTO,ZSABE,
KVERM,FDGRV,BUSAB,LNRZE,LNRZB,ZINDT,ZINRT,DATLZ,XDEZV,
WEBTR,KULTG,REPRF,TOGRU,HBKID,XPORE,QSZNR,QSZDT,QSSKZ,
BLNKZ,MINDK,ALTKN,ZGRUP,MGRUP,UZAWE,QSREC,QSBGR,QLAND,
XEDIP,FRGRP,TOGRR,TLFXS,INTAD,XLFZB,GUZTE,GRICD,GRIDT,
XAUSZ,CERDT,CONFS,UPDAT,UPTIM,NODEL,TLFNS,AVSND,AD_HASH,
J_SC_SUBCONTYPE,J_SC_COMPDATE,J_SC_OFFSM,J_SC_OFFSR,
BASIS_PNT,GMVKZK,PREPAY_RELEVANT,ASSIGN_TEST, _CELONIS_CHANGE_DATE
)
SELECT DISTINCT col1, col2, col3, ..., timestamp_col::DATE FROM LFB1
WHERE MANDT||LIFNR||BUKRS NOT IN (SELECT DISTINCT MANDT||LIFNR||BUKRS FROM LFB1_BACKUPTABLE);
Your syntax for casting to a date is syntactically correct, but in this example I used the more common Vertica syntax which is col::DATE, both will work.

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