Is it possible to insert a new row of data in a case statement in Hive.
I have a basic 'team' table, with the following fields (team_id,fname,lname).
This is what I am trying to run,
SELECT team_id,fname,lname,
CASE WHEN team_id = 2 THEN insert into team values (20, 'billy', 'bob'); ELSE "" END team_id
FROM team order by team_id;
Error ParseException line 2:29 Failed to recognize predicate 'insert'. Failed rule: 'identifier' in table or column identifier
If anyone could provide and info or solution, it would be great
Frostie
Afaik we can't put any ddl or dml operation in case statement in hive. But workaround can be applied to solve above problem, if really needs to be solved.
insert into table team select 20, 'billy', 'bob' from team where team_id = 2;
Explanation:- it will insert a new record in team table if team_id=2 else nothing to insert.
Related
I need to insert data of one table to another table. All the values are from the table except one SO_ID. It is coming from the Item on the page. How do I do it?
insert into T_SORDER_ITEM_INWARD
(
select sd.ID, SO_ID
into :P25_SO_ID, sd.STOCK_ID,sd.ITEM_ID,sd.UOM_ID,
sd.ITEM_CONDITION_ID,sd.ORIGINAL,sd.ACTUAL,sd.WIDTH,sd.LENGTH,sd.STOCKQTY,
sd.KANTA,sd.RATE,sd.PACKET, sd.LABEL_METER, sd.EXCESS_SHORT,sd.LOCATION_ID,
sd.CLIENT_INITIAL, sd.FIN_YEAR, sd.SERIAL_NO
from T_STOCK_DETAIL sd join t_stock_master sm
on sd.stock_id = sm.stock_id
where sm.customer_id = p25_customer
)
A simplified example:
insert into another_table (id, name, location)
select :P25_SO_ID,
t.name,
t.location
from this_table t
Always name all columns you're inserting into (first line in my example).
Your query is impossible to understand. Not just because syntax is wrong, but because we have no idea which column is supposed to get which value.
I am trying to replace all of the NULL values to 0 in a column of a big table in HIVE.
However, every time I try to implement some code I end up generating a new column to the table. The column I am trying to change/modify still exists and still has the NULL values but the new column that is automatically generated (i.e. _c1) is what I want the column I am trying to modify, to look like.
I tried to run a COALESCE but that also ended up generating a new column. I also tried to implement a CASE WHEN, but the same results ensued.
Select *,
CASE WHEN columnname IS NULL THEN 0
ELSE columnname
END
from tablename;
Also tried
SELECT coalesce(columnname, CAST(0 AS BIGINT)) FROM tablename
I would just like to update the table with the other columns being as is but the column I want to modify still has its original name but instead of NULL values it has 0's that replaced them.
I don't want to generate a new column but modify an existing one.
How should I do that?
Use insert overwrite .. option.
insert overwrite table tablename
select c1,c2,...,coalesce(columnname,0) as columnname
from tablename
Note that you have to specify all the other column names required in select.
I need to write a trigger in Oracle PL/SQL (11g) before inserting each row that checks if a row exists: if it doesn't exists creates a new row, if it does exists updates the existing record.
Which is the best way to do that?
Thanks, Gianluca
What you want to do is a MERGE INTO:
MERGE INTO myTable t
USING (SELECT 'Smith' AS Name, 1 AS Id FROM DUAL) data -- put your data in here
ON (t.Id = data.Id) -- pk or other matching criteria
WHEN MATCHED
THEN
UPDATE SET t.name = data.name
WHEN NOT MATCHED
THEN
INSERT (Id, Name)
VALUES (data.Id, data.Name);
Buildiing a trigger is possible but no allowed/intended. You should't do this.
You'd try to abbort the insert and do something else. This is not a good idea, because of many thing: hidden logic in db, stupid-clients which are doing wrong things..
You can abort with an error, but it doesn't sound like you idea this way.
If you want to do it you could change to update. Never insert anything and implement a trigger before update, which checks if the row exists:
CREATE OR REPLACE TRIGGER myTableTrigger
BEFORE UPDATE
ON myTable
FOR EACH ROW
BEGIN
-- If row doesn't exist. Insert one before the update..
END;
Alternatively you could go the long way and build some views:
https://dba.stackexchange.com/questions/24047/oracle-abort-within-a-before-insert-trigger-without-throwing-an-exception
Given the syntax:
CREATE [OR REPLACE] FUNCTION [Owner.]FunctionName
[(arguments [IN|OUT|IN OUT][NOCOPY] DataType [DEFAULT expr][,...])]
RETURN DataType [InvokerRightsClause] [DETERMINISTIC]
{IS|AS}
I think my query is syntactically correct, but for some reason, I get these errors during compilation:
Error(6,5): PL/SQL: SQL Statement ignored
Error(8,34): PL/SQL: ORA-00942: table or view does not exist
CREATE or replace FUNCTION aCombinationMismatches(p_column1 IN VARCHAR2)
RETURN Number
IS
duplicate_count NUMBER(4,0);
BEGIN
select count(*) into duplicate_count
from schema1.tableA a
inner join schema1.tableB b
on a.b_id = b.id and a.column1 = p_column1
group by a.b_id, a.column1, a.column2, b.column1, b.column2, b.column3;
return duplicate_count;
END;
Anyone see anything wrong with my query above?
Also I'd like to how to set this UDF up to be used to create a CHECK constraint. How exactly do I specify the param: p_param1 to the function assuming this is the value of a field column1 in a row that a user is trying to insert? I just don't want the user to insert a record into tableA that consists of duplicate combinations of fields across tables: tableA and tableB.
Note: The tables tableA and tableB do exist - a select query like below indicates it. So the error above is rather confusing to me, I must add. (All table and column names in the two queries were found/replaced with dummy values.)
select count(*)
from schema1.tableA a
inner join schema1.tableB b
on a.b_id = b.id
group by a.b_id, a.column1, a.column2, b.column1, b.column2, b.column3;
Output:
Count(*)
OK, you already know that you have problem with priviliges. I wanted to add that you won't be able to create CHECK constraint basing on your function. According to documentation:
The condition of a check constraint can refer to any column in the table, but it cannot refer to columns of other tables.
Conditions of check constraints cannot contain the following constructs:
Subqueries and scalar subquery expressions
Calls to the functions that are not deterministic (CURRENT_DATE, CURRENT_TIMESTAMP, DBTIMEZONE, LOCALTIMESTAMP, SESSIONTIMEZONE, SYSDATE, SYSTIMESTAMP, UID, USER, and USERENV)
Calls to user-defined functions
So to achieve what you want, you would have to define some triggers, or make use of some combination of MATERIALIZED VIEW and CHECK constraint. See for example this discussion on Ask Tom
You probably have access to TableA and TableB through a Role. This means that you can query the table, but you cannot create a procedure that reads or writes that table. In order to compile your procedure you should at least grant select on the table to your user.
In the link below you'll find more info
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1065832643319
The query:
MERGE INTO app_role_data x USING (select ? name, ? xml FROM dual) d ON (x.app_name = d.name) WHEN MATCHED THEN UPDATE SET x.xml_blob = d.xml WHEN NOT MATCHED THEN INSERT(app_name, xml_blob) VALUES(d.name, d.xml)
The table:
create table app_role_data(app_name varchar2(64), xml_blob clob);
The result: When a row exists, we get ORA-01461.
The desired goal: This is a "create or replace" operation on a row in this table, effectively. If 'name' exists in the table, then the xml column should be updated, else a new row inserted.
I think this turns out to be solved by using the Spring JDBC LOB setting functionality documented at 11.7.2 of the Spring Framework documentation.
However, that isn't working either... but will be the subject of another question.