ORA-00933 using case/when inside a select - sql

I'm trying to write this code into the toad, but it throws an ORA-00933 (SQL command not properly ended), just at the first WHEN.
I don't know exactly what happens. I've tried to follow the ORACLE docs, but with no success. Any help?
I let you the code written with toad.
select template.seq_temp, rules.fec_desde_serv, rules.seq_rec, rules.seq_agencia, rules.SEQ_TTOO, rules.COD_INTERFACE, pais.COD_PAIS, rules.cod_idioma
from re_t_vp_voucher_template template, re_t_vp_voucher_rules rules, re_t_vp_voucher_rules_pais pais,
case &tipser
when 'TRF' then re_t_vp_voucher_rules_trf trf
when 'ACC' then re_t_vp_voucher_rules_acc acct
else re_t_vp_voucher_rules_otro otro
end
where template.seq_temp = rules.seq_temp
and RULES.SEQ_RULE = PAIS.SEQ_RULE
group by template.seq_temp, rules.fec_desde_serv, rules.seq_rec, rules.seq_agencia, rules.SEQ_TTOO, rules.COD_INTERFACE, pais.COD_PAIS, rules.cod_idioma
order by template.seq_temp, rules.fec_desde_serv;

You cannot use CASE construct in FROM clause.
So your SQL as highlighted below is incorrect.
from re_t_vp_voucher_template template, re_t_vp_voucher_rules rules, re_t_vp_voucher_rules_pais pais,
case &tipser
when 'TRF' then re_t_vp_voucher_rules_trf trf
when 'ACC' then re_t_vp_voucher_rules_acc acct
else re_t_vp_voucher_rules_otro otro
end
where template.seq_temp = rules.seq_temp
The reason is that, you are trying to use dynamic table names in SQL, which is NOT allowed. You can't use dynamic table names in SQL unless you write dynamic SQL statements (i.e. build up the statement as a string in PL/SQL and then execute it using EXECUTE IMMEDIATE or the DBMS_SQL package).
So rewrite the query to meet the SQL standards, or (ab)use dynamic sql.
CASE is an expression, which works like IF-THEN-ELSE logic, and Oracle
uses short-circuit evaluation. so, it always needs a comparison
expression. Which you cannot have in FROM clause.

Your where condition is wrong.:-
where template.seq_temp = rules.seq_temp
and RULES.SEQ_RULE = PAIS.SEQ_RULE
case &tipser
when 'TRF' then and rules.seq_rule = trf.seq_rule
when 'ACC' then and rules.seq_rule = acct.seq_rule
else and rules.seq_rule = otro.seq_rule
end
Try to remove the 'and' keywords from there.

Related

Converting sql case expression

I'm trying to take the following sql case statement and convert it to Access 2010 as a calculation for a column. I've looked at the IIF statements but have received errors trying to sort it out. Thank you for any help.
case
when left(Tiers,4) = 'Tier' and isnumeric(right((left(Tiers,7)),2)) = 1 then right((left(Tiers,7)),2)
when left(Tiers,4) = 'Tier' and isnumeric(right((left(Tiers,7)),2)) = 0 then right((left(Tiers,6)),1)
else Tiers
end
;
you can only use Case statements in access in VBA Code.
and iif should work as well in access
like this
iif(left(Tiers,4) ='Tire',iif(isnumeric(right((left(Tiers,7)),2)) = 1,right((left(Tiers,7)),2),right((left(Tiers,6)),1)), Tires)
You can use IIF() as you mentioned and it will likely be a pain. There is also SWITCH function available which will likely make it easier for you to convert your case statement instead of nested IIF()s
Switch( expr-1, value-1 [, expr-2, value-2 ] … [, expr-n, value-n ] )
https://support.office.com/en-us/article/Switch-Function-d750c10d-0c8e-444c-9e63-f47504f9e379
This is a duplicate question of:
Case expressions in Access.

How to convert these SQL script code to CE function in SAP HANA

I want to transfer his SQL script code to CE functions:
select
A.MANDT,
A.ORGEH,
A.ORGLV,
A.S_PLANS,
A.S_PERNR,
b.ENAME as ENAME,
(case
when a.ORGLV = 'ROOT' then 'ROOT - ALL Chief'
when a.S_PERNR is null then ''
when b.ENAME is null then a.S_PERNR
else a.S_PERNR||' - '||b.ENAME end) as CA_LV_CHIEF
from : "Table1" as a left outer join "Table2" as b
on b.PERNR = a.S_PERNR
AND A."MANDT" = B."MANDT"
group by A.MANDT, A.ORGEH, A.S_PERNR, A.S_PLANS, A.ORGLV, b.ENAME
My question is about the calculated attribute CA_LV_CHIEF. You can see there are multiple when...then statements in the original SQL. I want to use CE_CALC() function to convert it to CE function, but I don't know how to nest CASE statement in it.
If possible, how can I do it; or if not possible, is there any other way to convert it to a CE function?
Thanks
The correct answer to this question is: don't do it!
Using CE functions has been clearly disencouraged by SAP.
With your SQL statement you have an easy to understand, straight forward to maintain and good optimizable statement.
Don't make it worse in every of those aspects by rewriting it as a CE function call.

If operator in groovy sql select statement

I would like to use the if operator in the select statement, using an executeQuery command.
Something like:
def tmpRec = BankStatement.executeQuery("select SUM(IF(operation='CREDIT',cashAmount,-1*cashAmount)) from BankStatement group by portfolio,currency,code")
go
but the IF operator doesn't work.
thank you
Although I do not know Groovy SQL, in most DBMS the CASE statement is used as a conditional statement. Perhaps the following will work:
def tmpRec = BankStatement.executeQuery("select SUM(CASE WHEN operation='CREDIT' THEN cashAmount ELSE (-1*cashAmount) END) from BankStatement group by portfolio,currency,code")

Access to T-SQL query conversion - What to use in place of the IIf() function?

My query is as follows :
I have written in access can anyone pls help how can I convert it into sql query I am getting an error near IIF condition.
SELECT #NT_VAR_STEP_1_1.SYS_ID, #NT_VAR_STEP_1_1.NODE, #NT_VAR_STEP_1_1.TEMP_ID,
#NT_VAR_STEP_1_1.EQUIP_TYPE, #NT_VAR_STEP_1_1.EQ_ID,
#NT_VAR_STEP_1_1.VAR_ID, #NT_VAR_STEP_1_1.NODE AS VAR_SET,
#NT_VAR_STEP_1_1.VAR_NAME, IIf([#NT_VAR_STEP_1_1]![VAR_SUBSET]=‘SELF’,
[#NT_VAR_STEP_1_1]![NODE],[#NT_VAR_STEP_1_1]![VAR_SUBSET]) AS VAR_SUBSET,
IIf([#NT_VAR_STEP_1_1]![EQUIP_TYPE]=‘SOURCE’,’PARAMVAR’,’VAR’) AS CALC_VAR_TYPE,
#NT_VAR_STEP_1_1.VAR_DATA_TYPE AS DATA_TYPE, #NT_VAR_STEP_1_1.DOF,
#NT_VAR_STEP_1_1.RETAIN, #NT_VAR_STEP_1_1.COEFF_OBJECT,
#NT_VAR_STEP_1_1.COEFF_VAR_SET, #NT_VAR_STEP_1_1.COEFF_VAR_TYPE,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBSET, #NT_VAR_STEP_1_1.COEFF_VAR_NAME,
#NT_VAR_STEP_1_1.OPERAND, #NT_VAR_STEP_1_1.SIGN,
#NT_VAR_STEP_1_1.VAR_TP_OFFSET, #NT_VAR_STEP_1_1.COEFF_TP_OFFSET,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBTYE INTO #OT_VAR_STEP_1_1_1
FROM #NT_VAR_STEP_1_1
GROUP BY #NT_VAR_STEP_1_1.SYS_ID, #NT_VAR_STEP_1_1.TEMP_ID,
#NT_VAR_STEP_1_1.EQUIP_TYPE, #NT_VAR_STEP_1_1.EQ_ID,
#NT_VAR_STEP_1_1.VAR_ID, #NT_VAR_STEP_1_1.NODE,
#NT_VAR_STEP_1_1.VAR_NAME, IIf([#NT_VAR_STEP_1_1]![VAR_SUBSET]=‘SELF’,
[#NT_VAR_STEP_1_1]![NODE],[#NT_VAR_STEP_1_1]![VAR_SUBSET]),
IIf([#NT_VAR_STEP_1_1]![EQUIP_TYPE]=‘SOURCE’,’PARAMVAR’,’VAR’),
#NT_VAR_STEP_1_1.VAR_DATA_TYPE, #NT_VAR_STEP_1_1.DOF,
#NT_VAR_STEP_1_1.RETAIN, #NT_VAR_STEP_1_1.COEFF_OBJECT,
#NT_VAR_STEP_1_1.COEFF_VAR_SET, #NT_VAR_STEP_1_1.COEFF_VAR_TYPE,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBSET, #NT_VAR_STEP_1_1.COEFF_VAR_NAME,
#NT_VAR_STEP_1_1.OPERAND, #NT_VAR_STEP_1_1.SIGN,
#NT_VAR_STEP_1_1.VAR_TP_OFFSET, #NT_VAR_STEP_1_1.COEFF_TP_OFFSET,
#NT_VAR_STEP_1_1.COEFF_VAR_SUBTYE, #NT_VAR_STEP_1_1.NODE
HAVING (((#NT_VAR_STEP_1_1.EQUIP_TYPE)<>‘COST_NODE’));
Convert this line:
IIf([#NT_VAR_STEP_1_1]![EQUIP_TYPE]=‘SOURCE’,’PARAMVAR’,’VAR’) AS CALC_VAR_TYPE
to this:
CASE WHEN [#NT_VAR_STEP_1_1].[EQUIP_TYPE]='SOURCE'
THEN 'PARAMVAR'
ELSE 'VAR'
END CALC_VAR_TYPE
And then use the same in your GROUP BY clause, but without the column alias:
CASE WHEN [#NT_VAR_STEP_1_1].[EQUIP_TYPE]='SOURCE'
THEN 'PARAMVAR'
ELSE 'VAR'
END
I will assume you're going to SQL Server (T-SQL). T-SQL does not support IIF, you will need to use CASE instead.

How to select if a row exists in HQL

EDIT: Specifically talking about querying against no table. Yes I can use exists, but I'd have to do
select case when exists (blah) then 1 else 0 end as conditionTrue
from ARealTableReturningMultipleRows
In T-SQL I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue
In Oracle I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue from DUAL
How can I achieve the same thing in HQL?
select count() seems like the second-best alternative, but I don't want to have to process every row in the table if I don't need to.
Short answer: I believe it's NOT possible.
My reasoning:
According to Where can I find a list of all HQL keywords? Hibernate project doesn't publish HQL grammar on their website, it's available in the Hibernate full distribution as a .g ANTLR file though.
I don't have much experience with .g files from ANTLR, but you can find this in the file (hibernate-distribution-3.6.1.Final/project/core/src/main/antlr/hql.g):
selectFrom!
: (s:selectClause)? (f:fromClause)? {
// If there was no FROM clause and this is a filter query, create a from clause. Otherwise, throw
// an exception because non-filter queries must have a FROM clause.
if (#f == null) {
if (filter) {
#f = #([FROM,"{filter-implied FROM}"]);
}
else
throw new SemanticException("FROM expected (non-filter queries must contain a FROM clause)");
}
which clearly states there are some HQL queries having no FROM clause, but that's acceptable if that's a filter query. Now again, I am not an expert in HQL/Hibernate, but I believe a filter query is not a full query but something you define using session.createFilter (see How do I turn item ordering HQL into a filter query?), so that makes me think there's no way to omit the FROM clause.
I'm use fake table with one row for example MyDual.
select case when exists(blah) then 1 else 0 end as conditionTrue from MyDual
According to http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions it looks like they support both case and exists statements.