Set hive variables based on condition - hive

I want to set hive variables based on condition. I am not sure if that's possible in HQL or not and if it is possible then how to achieve that.
I want to do something like this,
if ${hiveconf:work}==1 then do
set q1 = 4;
set yr2 = eval(${hiveconf:year}-1);
set q3= 3;
end
else if ${hiveconf:work}==2 then do
set q2 = 4;
set yr1 = eval(${hiveconf:year}-2);
set q4= 2;
end
Any help and knowledge will be appreciated.

Related

Using Sequel Pro: How do I update a column that contains a query?

So basically I have a column called 'query' which contains a query. I need to update that query but don't know how.
I tried a simple
UPDATE TABLE
SET QUERY = 'New Query'
WHERE ID = 1
but it's getting thrown out because of the pre-existing ' ' that I have in the query.
Way out of my league on this one. Trying to learn on the fly for a position that's going to open up.
You'll have to escape the ' in the query, possibly by doubling them up or using an escape char before them:
UPDATE t
SET query = 'DELETE FROM x WHERE y = ''z'' '
WHERE ID = 1
UPDATE t
SET query = 'DELETE FROM x WHERE y = \'z\' ' ESCAPE '\'
WHERE ID = 1
You can also make life easier/neater with REPLACE, like:
UPDATE t
SET query = REPLACE('DELETE FROM x WHERE y = "z" AND a = "b" AND c = "d" ', '"', CHAR(39))
This way you can use some char all through that doesn't occur naturally in the query, and replace it with ' at the end
I can't say which variant works for your DB because you didn't say what your DB was..
WHERE ID = 1

UPDATE query - multiple criteria for same field in MS Access

I am new to scripting in MS Access and would like to do multiple queries in one instance. I need this query to UPDATE records based off the SET criteria. Below is what I have so far.
UPDATE Inspections SET Inspections.Surveyed_By = "TDG/DT"
WHERE Inspections.Surveyed_By="TDG/DavidT";
OR
UPDATE Inspections SET Inspections.Surveyed_By = "TDG/RW"
WHERE Inspections.Surveyed_By="TDG/RobbieW"
Is this what you are looking for?
UPDATE Inspections
SET Surveyed_By = IIF(Surveyed_By = 'TDG/DavidT', 'TDG/DT', 'TDG/RW')
WHERE Surveyed_By IN ('TDG/DavidT', 'TDG/RobbieW');
You can use Switch
UPDATE Inspections
SET Surveyed_By = Switch(
Surveyed_By = "TDG/DavidT", "TDG/DT",
Surveyed_By = "TDG/RobbieW" , "TDG/RW" );
WHERE
Surveyed_By = "TDG/DavidT"
OR Surveyed_By = "TDG/RobbieW"

UPDATE same column multiple times in query

Is there a specified behavior for updating the same column 2+ times in the same UPDATE query, as follows?
UPDATE tbl SET a = 5, b = 'something', a = 6 WHERE c = 'whatever';
Is there a standardized behavior for this, or might it vary between flavors of SQL (e.g. it is "undefined behavior")? A cursory test with sqlite seems to indicate they are executed left-to-right, so the last column value will be the resulting one, but that doesn't imply that will always be the case.
Edit: The reason I'm trying to do this is I'm testing some SQL injection for a class project. One of the fields in an UPDATE is unsafely injected, and I'm trying to use it to overwrite previously SET fields from the same query.
This isn't exactly the answer you're looking for but assuming that the text "something" is a field you are passing in and it isn't parameterized or escaped you may be able to do this. This all depends on how the query is being built and what database it is being run against.
UPDATE tbl SET a = 5, b = 'something'; UPDATE tbl set a = 6;--' WHERE c = 'whatever';
by entering the following in the user input
something'; UPDATE tbl set a = 6;--
This assumes that the query is built dynamically something like this
var query = "UPDATE tbl set a = 5, b = '" + userInput + "' WHERE c = 'whatever'";
Here is a relevant question: How does the SQL injection from the "Bobby Tables" XKCD comic work?

Replace multiple fields with one query

Instead of running two separate queries to replace the innerSku and outerSku fields in my parts table, is there a way to do this in one query without affecting performance?
UPDATE
parts
SET
innerSku = #newSku
WHERE
innerSku = #oldSku;
UPDATE
parts
SET
outerSku = #newSku
WHERE
outerSku = #oldSku;
The case statement becomes your where condition determining what value to set, only updating the column that meets the condition, otherwise it will just set the current value back.
The where clause makes it so you don't run the statement on your whole table.
SET parts.innerSku = (CASE WHEN innerSku = #oldSku THEN #newSku ELSE innerSku END),
parts.outerSku = (CASE WHEN outerSku = #oldSku THEN #newSku ELSE outerSku END)
WHERE parts.innerSku = #oldSku or parts.outerSku = #oldSku
I would stick to two separate statements, since using following will update some rows which doesnt need to be updated, it will update the rows to itself, which will not change the data but unnecessary updates operations will take place, I would say stick to two updates. anyway following is how you can update this table with one update statement.
UPDATE Parts
SET innerSku = CASE
WHEN innerSku = #oldSku
THEN #newSku ELSE innerSku
END
,outerSku = CASE
WHEN outerSku = #oldSku
THEN #newSku ELSE outerSku
END
WHERE innerSku = #oldSku OR outerSku = #oldSku

SQLITE bulk UPDATE statement

I want to perform many SQL UPDATE Statements like these:
UPDATE cityd SET time_zone='-7.000000' WHERE locId = 173567;
UPDATE cityd SET time_zone='-8.000000' WHERE locId = 173568;
UPDATE cityd SET time_zone='-6.000000' WHERE locId = 173569;
UPDATE cityd SET time_zone='-5.000000' WHERE locId = 173570;
UPDATE cityd SET time_zone='-6.000000' WHERE locId = 173571;
I want to optimize the transaction time, so I need to use BEGIN TRANSACTION/COMMIT pair. How to write this in SQL Syntax in SQLtite Manager?
Edit:
When I use the standard syntax in SQLite Manager I receive this error message:
"SQLiteManager: BEGIN TRANSACTION; [ cannot start a transaction within a transaction "
According on SQL Documention, there are two supported syntax of CASE
CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END
CASE WHEN x = w1 THEN r1 WHEN x = w2 THEN r2 ELSE r3 END
So your multiple UPDATE statements can be further simplified into
UPDATE cityd
SET time_zone = CASE locId
WHEN 173567 THEN '-7.000000'
WHEN 173568 THEN '-8.000000'
WHEN 173569 THEN '-6.000000'
WHEN 173570 THEN '-5.000000'
WHEN 173571 THEN '-6.000000'
END
WHERE locId IN (173567, 173568, 173569, 173570, 173571)
BEGIN TRANSACTION;
.....YOUR SQL Statements here
COMMIT;