Hive Variables in Where Clause - hive

SELECT * FROM Tablename
WHERE 1=1
(AND COL1=VALUE1 AND COL2=VALUE2) --->$(VAR)
SELECT * FROM Tablename
WHERE 1=1
$(VAR)
This is possible in Hive?

Yes, it is possible.
set hivevar:var1='and col1=10 and col2=10';
! echo "select * from table where 1=1 ${hivevar:var1}";
select ${hivevar:var1}
[prjai#lnx0689 prvys]$ hive -f test.hql
"select * from table where 1=1 'and col1=10 and col2=10'"
OK
and col1=10 and col2=10
Time taken: 2.152 seconds, Fetched: 1 row(s)
For more details on how to use hivevar and hiveconf, refer
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+VariableSubstitution
What is the difference between -hivevar and -hiveconf?

Set hive variable variable first.
set hivevar:queryPortion='and col1=value1 and col2=value2';
After that just retrieve it in your query
select * from table where 1=1 ${queryPortion}

Related

Run Hive Script with variable - problem with passing variable

I'm using DBVisualiser to run the following script (Expecting to pass variable in query and retrieve data based on where condition)
set Id='1';
select * from MyTable where account_id = '${hivevar:Id}' limit 5
Unfortunatelly, when I run this script I see that query which is executed is as follows:
set Id='1';
select * from mytable where account_id = '${hivevar:Id}' limit 5
But when I run query with hardcoded value
select * from mytable where account_id = '1' limit 5
then I get expected dataset.
I would apprecaie if anyone could help me to learn what I do wrong.
Thanks in advance.
Variables which were set without namespace are hiveconf variables, not hivevar. Though you can specify the namespace explicitly.
Try this:
set Id=1; --No need to quote here if it is quoted in the select
-- use hiveconf
select * from MyTable where account_id = '${hiveconf:Id}' limit 5;
Or this:
--specify the namespace
set hivevar:Id=1;
select * from MyTable where account_id = '${hivevar:Id}' limit 5;
See also similar question.

Bigquery + Firebase analytics: select table of yesterday

I'm querying bigQuery like this:
SELECT
param1.value.string_value AS id,
count(1) AS views,
FROM `projectname.analytics_id.events_20200808`,
UNNEST(event_params) as param1
WHERE
event_name = 'myEvent'
AND param1.key='id'
GROUP BY 1
I would like to make this a scheduled query. Is there a possibility to make my table a variable (so I can query the table of yesterday automatically)?
projectname.analytics_id.events_20200808 -> projectname.analytics_id.events_VARIABLE
Thanks!
You can use dynamic SQL option by using EXECUTE IMMEDIATE. Here is the example:
DECLARE TABLE_NAME STRING;
SET TABLE_NAME=CONCAT("projectname.analytics_id.events_",FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)));
EXECUTE IMMEDIATE FORMAT("""SELECT * FROM %s""",TABLE_NAME);

spool for insert in sql developer not working

I have below select query for which the result of this query i want to create insert scripts and saved it in files. I have used spool.
set long 10000
set lines 100000
set sqlformat insert
spool c:\temp\Insert_TEST_GRP.sql
select ID,NAME,TEST_DATE from TEST_GRP sd
where TEST_DATE =
( select min(TEST_DATE)
from TEST_GRP sd2
where sd.ID = sd2.ID
)
and sd.TEST_DATE <> TO_DATE ('01.01.2000', 'dd.mm.yyyy');
spool off
The file has been created. But when i view the file i am getting the result which is not in the form of insert statements as i want to run this insert statement again.
Below hows the data looks like in file which looks in incorrect format:
We don't have access to your table or your data.
But here it is working with the demo schema HR and its EMPLOYEES table
set sqlformat insert
spool c:\users\jdsmith\desktop\SO_inserts.sql
select * from employees;
spool off
You're using SET LONG - does your table have LOBS in it?
Also, I noticed you asked this same question on the OTN Forums...
The set sqlformat method to format your query results was added in version 4.1.
If you're on an earlier version (e.g. 3.0 as you said in a comment) then it would complain, which you seem to have overlooked; e.g. in 4.0:
set sqlformat insert
gets this in the script output window:
line 1: SQLPLUS Command Skipped: set sqlformat insert
The /*insert*/ method was available earlier than that:
select /*insert*/ * from dual;
which gets
REM INSERTING into dual
SET DEFINE OFF;
Insert into "dual" (DUMMY) values ('X');
(don't really attempt to insert into dual, of course). You can also use the export wizard (tools->database export); or run your query with control-enter, right-click on the output grid and choose 'export' (though it may repeat the query).
Upgrading to the current version is the sensible thing to do though.
You need to return a string that is the INSERT statement formatted with the columns you need. Example
set long 10000
set lines 100000
set sqlformat insert
spool c:\temp\Insert_TEST_GRP.sql
select 'INSERT INTO TEST_GRP (ID,NAME,TEST_DATE) VALUES (' ||
ID||','||NAME||',' || TEST_DATE||');'
from TEST_GRP sd
where TEST_DATE =
( select min(TEST_DATE)
from TEST_GRP sd2
where sd.ID = sd2.ID
)
and sd.TEST_DATE <> TO_DATE ('01.01.2000', 'dd.mm.yyyy');
spool off
If you are using sqldeveloper, then you can just use the built-in export function and export the result grid as inserts.
I used SQL Developer. Make sure to click Run script(F5) instead of run statement.
For multi statement in sample file use "/" between statement.
SET FEEDBACK OFF
set sqlformat insert
spool C:\Necessary_file\Reqular_task\QDE\profile_const.sql
select * from export_profile where profile_name='SPS_DIAG';
/
select * from profile_const where profile_name='SPS_DIAG';
/
select * from profile_panel where profile_name='SPS_DIAG' order by 5
/
spool off

How to TRUNCATE 100 TABLES At a time in SQL

I just want to delete the data not the structure.
Generate a TRUNCATE TABLE statement for each table and execute it
You can do this is many ways, some of which are
Use a loop and dynamic SQL
Copy the table names to excel and use it to generate the statement
Generate a script.
Oracle example:
set head off
set pagesize 0
spool t.sql
select 'truncate table '||table_name||';' from all_tables
order by table_name
where rownum < 100
Or if SQL Server, or an ANSI approach is required, use sys.tables or INFORMATION_SCHEMA.tables
select top 100
'truncate table ' + table_name + ';'
from INFORMATION_SCHEMA.tables;
MySQL
select CONCAT("truncate table ", table_name, ";")
from INFORMATION_SCHEMA.tables
limit 100;

Is it possible to apply SELECT INTO a temporary table from another SELECT?

For some performance improvements, I am looking at using a temporary table rather than a table variable
I am currently putting 100,000s or rows into a table variable using INSERT INTO #table EXECUTE sp_executesql #SQLString (where #SQLString returns a string 'SELECT 'INSERT INTO LiveTable Values('x','y','z') build by dynamic SQL so that the x,y,z values are from the real records)
The INSERT INTO takes a bit of time and I was wondering if, having read about how much better SELECT * INTO #tempTable is, can you do a SELECT * INTO with another SELECT as the source?
So something like
SELECT * INTO #tempTable FROM (SELECT * FROM Table2)
The problem with your query is that all subqueries need a table alias in SQL:
SELECT *
INTO #tempTable
FROM (SELECT * FROM Table2) t;
Short answer is yes (I believe I have done this before, awhile ago, but I don't recall any issues). You can get some more information from this post on msdn:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/92e5fdf0-e2ad-4f1c-ac35-6ab1c8eec642/select-into-localvarname-from-select-subquery
SELECT * INTO #tempTable FROM (SELECT * FROM Table2)T
SELECT * INTO #tempTable FROM Table2
SELECT * INTO #TempTable
FROM table_name
Yes you can do this, Point to be noted is this #TempTable will be created on the fly, meaning if there is a Temp table that already exists using this error will throw an error as it will try to create a Temp table 1st and then insert the data into it.
To insert data into a table that already exist you will have to use INSERT INTO syntax, something like
INSERT INTO #TempTable --<-- When using this syntax it is best practice to always
SELECT Col1, COl2, .... -- mention the Column names in INSERT INTO and SELECT
FROM TableName -- rather then using SELECT * to makes sure data is being
-- selected from and insert into the RIGHT columns
since you have mentioned you are using it with a stored procedure and would like to use make use of this syntax with store procedure, Im sorry you will not be able to do anything like
SELECT * INTO #Temp Execute usp_Proc
for this you will have to stick with
INSERT INTO #TempTable Execute usp_Proc
The Temp table has to exists before you can insert data into it from a stored Procedure.