Run Hive Script with variable - problem with passing variable - variables

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.

Related

How to create a reference value in Oracle SQl Developer

I use basic sql querying for my day to day work, but I regularly find myself needing to run queries in different tables using the same where clauses.
What I would ideally like to do is locally set a value to a name, for example:
traderef = ABCD1234. It's kind of like Defining a name in excel.
and then use 'traderef' in my queries,
select * from table1 where tranid = traderef
select * from table2 where tranid = traderef and otherattr = 'xyz1'
I only have query access to the dbs that i use, i have tried to google results, and found some info re SET
TIA
Declare a bind variable using the SQL/Plus and SQL Developer command VARIABLE name data_type and assign it a value using EXECUTE (or a PL/SQL anonymous block) and then use it in your queries:
VARIABLE traderef VARCHAR2(20)
EXEC :traderef := 'ABCD1234';
SELECT * FROM table1 WHERE tranid = :traderef;
SELECT * FROM table2 HWERE tranid = :traderef AND otherattr = 'xyz1';

How to set variables in Dremio

How do you set a variable in dremio?
In sql, normally you can do something like:
SET #ID = (SELECT id FROM table LIMIT 1)
or
SELECT #ID = (SELECT id FROM table LIMIT 1)
This does not seem to work for Dremio query. Anybody know how variables work in dremio?
This is not possible. We can't set variables in Dremio.
Dremio uses ANSI SQL where there is no such construct.

declare variable in sql (hive)

I had a deep look on the internet, but I'm not able to find any suitable answer.
In hive, is it possible to declare a variable, lets say:
test = 1
And change the value of this variable inside a query?
select
case
when field > 1 then test = test+1
else test = 1
end as test
from my table
It is possible. Please find the below code to create a variable in Hive.
hive> SET cust_id = 1234567890;
Once you create variable you can use it in your query like below.
hive> select * from cust_table where customer_id = '${hiveconf:cust_id}';
Hope this will help you.
Now you can apply this to your scenario.

How to test run an UPDATE statement in PostgreSQL?

How can I test an UPDATE statement for example to see if it would work, for example if it would actually update rows etc?
Is there a way to simulate it easily?
Use a transaction to wrap your update statement and a select query (to test the update) and then always roll it back.
Example:
BEGIN;
UPDATE accounts SET balance = balance - 100.00
WHERE name = 'Alice';
SELECT balance FROM accounts WHERE name = 'Alice';
ROLLBACK; -- << Important! Un-does your UPDATE statement above!
A transaction typically ends with a commit but since you're just testing and do not want the changes to be permanent you will just roll back.
Wrap it in a transaction, test the results with a SELECT and rollback at the end.
BEGIN;
UPDATE ...;
SELECT ...;
ROLLBACK;
Prepend your SQL UPDATE command with EXPLAIN [ref], and it will tell you how many lines will be affected by your command. And it will not perform the actual update.
This is much simpler than wrapping your command in a transaction. E.g.:
EXPLAIN UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Alice';
You could always build up a sample database on SQL Fiddle and try out your update statements there.
Full disclosure: I am the author of sqlfiddle.com
With Postgres you can use the UPDATE clause RETURNING to show which rows have been modificated.
-- example data
CREATE TABLE data(id int, text text);
INSERT INTO DATA VALUES(1,'aaa'),(2,'bbb'),(3,'ccc'),(4,'ddd');
-- original data
SELECT * from data;
-- dry-run update
BEGIN;
UPDATE
data
SET
text = 'modified'
WHERE
id > 2
RETURNING
id, text;
ROLLBACK;
-- data after dry-run update
SELECT * from data;
Run the same check with a SELECT statement first: the rows returned by SELECT will be the rows modified by UPDATE
Given this simple update:
UPDATE Products
SET price_including_vat = price * 1.05
WHERE product_type = 'Food';
I would test it using something like this:
SELECT price_including_vat AS price_including_vat__before,
price * 1.05 AS price_including_vat__after,
*
FROM Products
WHERE product_type = 'Food';
Actually, I'd proably engage brain and do analysis more like this:
WITH updated AS
(
SELECT price_including_vat AS price_including_vat__before,
price * 1.05 AS price_including_vat__after,
*
FROM Products
WHERE product_type = 'Food'
)
SELECT *
FROM updated
WHERE price_including_vat__before = price_including_vat__after;

Get the number of affected rows in a MySQL update statement?

I have stored procedure in MySQL, something like the below:
create procedure SP_Test (input1 varchar(20))
begin
update Table1 set Val1='Val' where country=input1;
//I want to see if this update changed how many rows and
//do some specific action based on this number
....
end
How can I determine how many rows were changed by this update?
Use ROW_COUNT():
SELECT ROW_COUNT();
one way, not very optimal is to simply do a select before you do the update.
select count(*) from table1 where country = 'country1'
Try the following code:
int mysql_affected_rows ([ resource $link_identifier = NULL ] )