Derby run sql script by redirecting standardIO - sql

I have the following join.sql script:
connect 'jdbc:derby:barra';
show tables;
create table sp500_univ as
select a.*,b.* from (select * from LEFT_SIDE) as a
left join (select * from RIGHT_SIDE) as b
on a.cmp_flg = b.cmp_flg2;
disconnect;
exit;
which I run with the following command:
java org.apache.derby.tools.ij < join.sql
and get the following output:
java org.apache.derby.tools.ij < join.sql
ij version 10.14
ij> ij> TABLE_SCHEM |TABLE_NAME |REMARKS
------------------------------------------------------------------------
APP |LEFT_SIDE |
APP |RIGHT_SIDE |
2 rows selected
ij> > > > ERROR 42X01: Syntax error: Encountered "<EOF>" at line 4, column 25.
Issue the 'help' command for general information on IJ command syntax.
Any unrecognized commands are treated as potential SQL commands and executed directly.
Consult your DBMS server reference documentation for details of the SQL syntax supported by your server.
ij> ij>
If I run this sql right from the command line in IJ it works.

apparently when running from a file you can't create tables and load data from a select statement. You need to add the WITH NO DATA. The WITH DATA option has not yet been implemented. From Derby's documentation:
CREATE TABLE ... AS ...
With the alternate form of the CREATE TABLE statement, the column names and/or the
column data types can be specified by providing a query. The columns in the query
result are used as a model for creating the columns in the new table.
If no column names are specified for the new table, then all the columns in the
result of the query expression are used to create same-named columns in the new
table, of the corresponding data type(s). If one or more column names are specified
for the new table, then the same number of columns must be present in the result of
the query expression; the data types of those columns are used for the corresponding
columns of the new table.
The WITH NO DATA clause specifies that the data rows which result from evaluating the
query expression are not used; only the names and data types of the columns in the
query result are used. The WITH NO DATA clause must be specified; in a future
release, Derby may be modified to allow the WITH DATA clause to be provided, which
would indicate that the results of the query expression should be inserted into the
newly-created table. In the current release, however, only the WITH NO DATA form of t
the statement is accepted.

Related

Drop tables using table names from a SELECT statement, in SQL (Impala)?

How do I drop a few tables (e.g. 1 - 3) using the output of a SELECT statement for the table names? This is probably standard SQL, but specifically I'm using Apache Impala SQL accessed via Apache Zeppelin.
So I have a table called tables_to_drop with a single column called "table_name". This will have one to a few entries in it, each with the name of another temporary table that was generated as the result of other processes. As part of my cleanup I need to drop these temporary tables whose names are listed in the "tables_to_drop" table.
Conceptually I was thinking of an SQL command like:
DROP TABLE (SELECT table_name FROM tables_to_drop);
or:
WITH subquery1 AS (SELECT table_name FROM tables_to_drop) DROP TABLE * FROM subquery1;
Neither of these work (syntax errors). Any ideas please?
even in standard sql this is not possible to do it the way you showed.
in standard sql usually you can use dynamic sql which impala doesn't support.
however you can write an impala script and run it in impala shell but it's going to be complicated for such task, I would prepare the drop statement using select and run it manually if this is one-time thing:
select concat('DROP TABLE IF EXISTS ',table_name) dropstatements
from tables_to_drop

Hive conditionally select column name

I have multiple tables with very similar schema except one column, which can have different names.
I want to make some complicated calculations using Hive and would like to have one code for all tables with possible parametrisation. For some reasons, I can't parametrise queries using language like Python, Scala etc, so decided to go with pure Hive SQL.
I want to conditionally select appropriate column, but it seems, that Hive evaluates all parts of conditional expression/statement regardless of condition.
What did I wrong?
DROP TABLE IF EXISTS `so_sample`;
CREATE TABLE `so_sample` (
`app_version` string
);
SELECT
if (true, app_version, software_version) AS firmware
FROM so_sample
;
Output:
Error: Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 2:25 Invalid table alias or column reference 'software_version': (possible column names are: app_version) (state=42000,code=10004)
Regards
Pawel
Try to use regex to select the column with different names, for more information see manual and don't forget
set hive.support.quoted.identifiers=none;

Issue while updating column using Merge statement

I need to update a column based on the inner join of two tables in oracle. I am using Merge as the inner join doesn't support in Update.
Query :
MERGE INTO FAC.CMC_PRSQ_SITE_QA PRSQ
USING(
SELECT PRPR_ID, ADDRESS_TYPE, PRAD_EFF_DT, PRAD_TERM_DT
FROM FSG_WRK.FSG_PRAD_PRSQ_TEMP
) TEMP
ON (TEMP.PRPR_ID = PRSQ.PRPR_ID
AND TEMP.ADDRESS_TYPE = PRSQ.PRAD_TYPE
AND TEMP.PRAD_EFF_DT = PRSQ.PRAD_EFF_DT)
WHEN MATCHED THEN UPDATE
SET PRSQ.PRSQ_NEXT_VER_DT = TEMP.PRAD_TERM_DT
WHERE TEMP.PRAD_TERM_DT > PRSQ.PRSQ_LAST_VER_DT ;
But getting below error :
ORA-30926: unable to get a stable set of rows in the source tables
ORA-06512: at line 2
30926. 00000 - "unable to get a stable set of rows in the source tables"
*Cause: A stable set of rows could not be got because of large dml
activity or a non-deterministic where clause.
*Action: Remove any non-deterministic where clauses and reissue the dml.
Also, please let me know if there is any other option to rewrite this query.
Does your source query (TEMP) return more than 1 instance of a row in your destination (PRSQ) that matches your merge conditions? You typically see this type of error when the source contains more than 1 instance of a row that needs to be updated in the destination. If it does you need to adjust either your source query or your merge condition to avoid trying to merge multiple rows from your source into the same row of your destination.

Using values from two tables to run query in Hive

I would like to run a hive query to be able to divide a column from one table by the total sum of a column from another table.
Do I have to join the tables?
The code below generates errors:
Select 100*(Num_files/total_Num_files) from jvros_p2, jvros_p3;
FAILED: Parse Error: line 1:75 mismatched input ',' expecting EOF near 'jvros_p2'
Yes, jvros_p3 is a single row single column table
Num_files is a column in jvros_p2 and total_Num_files is a single value in jvros_p3.
Your older version may be why your notation isn't working. Try this:
SELECT 100 * (Num_files / total_Num_files) FROM jvros_p2 JOIN jvros_p3;
I suspect that if you are eventually able to upgrade to at least 0.13, implicit join notation via comma-separated tables will be supported per HIVE-5558.

How to dynamically execute sql statement

I want to execute an sql statement dynamically based on the column passed by user through jTextField.
str=select "+jTextField.getText()+" from table ;
This statement works fine of the column name does not have a space.
Example : suppose I have two columns = Priority and Request Type.
This above statement works perfect if I type priority but fails when I use request type
and
if I use the statement
str=select ["+jTextField.getText()+"] from table ;
then Request Type entry will work fine but Priority won't.
Any idea to resolve this issue.
You may need to trim trailing/leading spaces from the field name, otherwise it should work fine:
str=select ["+jTextField.getText().trim()+"] from table ;
Are you sure Priority is the right field name within that table?
Also be aware that you are vulnerable to SQL injection. Suppose you put the following in jTextField:
"null] FROM table; DROP TABLE table; --"
Then the result is
select [null] FROM table; DROP TABLE table; --] from table ;