output/echo a meesage in hql/ hive query language - hadoop

I need to create a hive.hql as follows.
HIVE.hql:
select * from tabel1;
select * from table2;
My question is: can i echo any message to my console like " results from table1 is obtained " in the hql code after table one is created like
select * from tabel1;
echo/print/output ("table 1 results obtained");
select * from table2;

In the *.hql file insert a line as below in between the two hive queries.
!echo "table 1 results obtained";

You can add a comment by editing your HIVE.hql :
select * from tabel1;
!sh echo "table 1 results obtained";
select * from table2;

In beeline just use the below
--add your comment after 2 hypens and its printed in the console
Other solutions didnt work probably they are removed in current version

Related

Can I define a variable for a list of values in SqlPlus

I have a script (for Oracle) with a number of sql statements that all target the same list of values, for example
select * from s1.t1 where f1 in ('val1', 'val2')
select * from s2.t2 where f2 in ('val1', 'val2')
-- etc.
Instead of pasting this list of values in at each place in the script, I would like to define a variable to hold that list, and then use it in the correct place, something like:
define var1 = "('val1','val2')"
select * from s1.t1 where f1 in &&var1
select * From s2.t2 where f2 in &&var1
When I attempt it the way it's written above, I get the error "ORA-00933: SQL command not properly ended", and the verified statement in the log looks like:
select * from s1.t1 where f1 in '('val1','val2')'
So I can see that the variable substitution is putting single quotes around my entire variable value, which is sort of reasonable but not what I want. Is there a way to get done what I want here?
Your 'verified statement' ends up as:
select * from s1.t1 where f1 in '('val1','val2')'
which means that, if you've defined the substitution variable as you said, your query is actually then using it as:
select * from s1.t1 where f1 in '&&var1';
old:select * from s1.t1 where f1 in '&&var1'
new:select * from s1.t1 where f1 in '('val1','val2')'
SQL Error: ORA-00933: SQL command not properly ended
Enclosing the variable in single quotes is necessary when the value is an unquoted string, which is usually the case with new_value and by default with define or accept or when prompted for the variable value. But here your variable value already has all the quotes you need, so it should just be as you actually showed it in your question, without the quotes:
select * from s1.t1 where f1 in &&var1;
old:select * from s1.t1 where f1 in &&var1
new:select * from s1.t1 where f1 in ('val1','val2')
which is now valid.
Works OK for me; example based on Scott's EMP table:
SQL> define vjob = "('CLERK', 'MANAGER')"
SQL> select count(*) from emp where job in &vjob;
old 1: select count(*) from emp where job in &vjob
new 1: select count(*) from emp where job in ('CLERK', 'MANAGER')
COUNT(*)
----------
7

How can i "Clean" output from sqlplus query?

i have got simple query and i need just one value from it = VALID
The query is:
select 'VALUE('||status||')' as value from user_indexes where index_name = '&1';
But i hve got in out:
C:\Program Files\zabbix\bin\win64\oracle>sqlplus -s #"C:\Program Files\zabbix\bi
n\win64\oracle\conn2.sql" OLAPTABLEVELSID
old 1: select status from user_indexes where index_name = '&1'
new 1: select status from user_indexes where index_name = 'OLAPTABLEVELSID'
VALID
What are this OLD and NEW strings? How can i dismiss it?
Thank you.
SET VERIFY OFF should help you. Please add such line in your script before query.
in sqplus you have substition variables. they are referenced by & or &&
when you run your script and pass it 'OLAPTABLEVELSID' - the query takes the '&1' and replaces it with ''OLAPTABLEVELSID'
SQL*Plus is telling you that in the output
SQL> set verify off
SQL> select '&1' from dual;
Enter value for 1: hello stackoverflow
'HELLOSTACKOVERFLOW
-------------------
hello stackoverflow
SQL>

how to get multiple values from different tables plsql

I usually do this query every day:
select * from table1
where name = 'PAUL';
select * from table2
where id_user = 012345;
select * from table25
where name = 'PAUL';
select * from table99
where name = 'PAUL';
select * from table28
where id_user = 012345;
.
.
.
I do this query every day. Today is 15 tables, tomorrow maybe 20, the only change is the name or id of the user to search. I have to go placing the username or id in each of the queries, so I need a query where there is a variable and assign the name and id.
.
.
There is a way to simplify this query and make it better? Such as:
DECLARE
l_name table1.name %type;
l_id_user table28.id_user %type;
BEGIN
l_id_user := 012345;
l_name := 'PAUL';
select * from table1
where name in ('l_name');
select * from table2
where id_user in (l_id_user);
.
.
.
END;
I tried that way but fails. I need this query because in most cases need to see up to 20 tables or more.
If what you want is an easy way to repeatedly run a sequence of select statements in SQL*Plus - but with varying criteria you can do it like this:
ACCEPT l_name PROMPT "Input user name: "
ACCEPT l_id_user PROMPT "Input user id: "
select * from table1
where name = '&l_name';
select * from table2
where id_user = &l_id_user;
select * from table25
where name = '&l_name';
select * from table99
where name = '&l_name';
select * from table28
where id_user = &l_id_user;
In a pl/sql block declare ... begin .. end; select statements behave differently from when you run them stand alone in SQL*Plus:
They won't automatically print output.
You must use e.g. a FOR LOOP or a variation of the SELECT INTO syntax to capture the result for subsequent processing. You should seek some documentation on pl/sql programming.
You should understand that pl/sql is definitely not designed for screen output.
It is possible to print out string values using dbms_output.put_line, but you are completely on your own with regards to formatting a row of values into a suitable string.
If your result contains more than a few columns formatting for screen output becomes very cumbersome as you wont have any formatting utilities like the java String.format() to assist you.
And if your result contains more than one row you have to provide some sort of loop construct allowing you to print out the individual rows.
Conlusion:
These are the drawbacks of pl/sql - I really don't see any benefits - unless your aim is to apply some logic to the data being read rather than just printing it to the screen or a file.

pl-sql include column names in query

A weird request maybe but. My boss wants me to create an admin version of a page we have that displays data from an oracle query in a table.
The admin page, instead of displaying the data (query returns 1 row), needs to return the table name and column name
Ex: Instead of:
Name Initial
==================
Bob A
I want:
Name Initial
============================
Users.FirstName Users.MiddleInitial
I realize I can do this in code but would rather just modify the query to return the data I want so I can leave the report generation code mostly alone.
I don't want to do it in a stored procedure.
So when I spit out the data in the report using something like:
blah blah = MyDataRow("FirstName")
I can leave that as is but instead of it displaying "BOB" it would display "Users.FirstName"
And I want to do the query using select * if possible instead of listing all the columns
So for each of the columns I am querying in the * , I want to get (instead of the column value) the tablename.ColumnName or tablename|columnName
hope you are following- I am confusing myself...
pseudo:
select tablename + '.' + Columnname as WhateverTheColumnNameIs
from Table1
left join Table2 on whatever...
Join Table_Names on blah blah
Whew- after writing all this I think I will just do it on the code side.
But if you are up for it maybe a fun challenge
Oracle does not provide an authentic way(there is no pseudocolumn) to get the column name of a table as a result of a query against that table. But you might consider these two approaches:
Extract column name from an xmltype, formed by passing cursor expression(your query) in the xmltable() function:
-- your table
with t1(first_name, middle_name) as(
select 1,2 from dual
), -- your query
t2 as(
select * -- col1 as "t1.col1"
--, col2 as "t1.col2"
--, col3 as "t1.col3"
from hr.t1
)
select *
from ( select q.object_value.getrootelement() as col_name
, rownum as rn
from xmltable('//*'
passing xmltype(cursor(select * from t2 where rownum = 1))
) q
where q.object_value.getrootelement() not in ('ROWSET', 'ROW')
)
pivot(
max(col_name) for rn in (1 as "name", 2 as "initial")
)
Result:
name initial
--------------- ---------------
FIRST_NAME MIDDLE_NAME
Note: In order for column names to be prefixed with table name, you need to list them
explicitly in the select list of a query and supply an alias, manually.
PL/SQL approach. Starting from Oracle 11g you could use dbms_sql() package and describe_columns() procedure specifically to get the name of columns in the cursor(your select).
This might be what you are looking for, try selecting from system views USER_TAB_COLS or ALL_TAB_COLS.

Using substitution variables in table name

My issue is easy, I want to find out if what I am trying to do can be done or not.
I have tables like this : detailcro1, detailcro2, detailcro3 ... I want to use a substitution var for some automatic process.
I wrote
DEFINE TT = 'detailcro'
select * from &TT||'2';
and as a result I have ORA-00933:.
Can I create such a query ?
Thank you
You need to usu a dot notation at the end of the variable
SQL> select * from &TT.l;
Enter value for tt: dua
old 1: select * from &TT.l
new 1: select * from dual
D
-
X
so
SQL> DEFINE TT = 'detailcro';
SQL> select * from &TT.2;
old 1: select * from &TT.2
new 1: select * from detailcro2
no rows selected

Resources