Log errors into error table in insert statement - sql

I came across an error invalid identifier in the error table even though the fields are present in the error table. The below is my test code for your reference. Any update absolutely appreciated.
Insert into test1
(a,b,e)
Select a,b,c||d e from
(Select c.a, d.b, d.c, c.d
From
(select e.a,e.b, f.c, d.d from
Test2 e, Test 2 f where
e.id = f.id) c ) v
Log errors into test1_err
(‘Batch :’
|| to_char(v.a)
|| to_char(v.b)
|| ‘;’
) Reject Limit Unlimited;
SQL Error: ORA:-00904: "v.a": invalid indentifier

Supposing you created the table with the dbms_errlog package, the expression you put in the '()' can contain the following as per the doc:
simple_expression
Specify the value to be used as a statement tag, so that you can identify the errors from this statement in the error logging table. The expression can be either a text literal, a number literal, or a general SQL expression such as a bind variable. You can also use a function expression if you convert it to a text literal — for example, TO_CHAR(SYSDATE).
so you cannot put column values. These are added to the table by the kernel, no need to add them yourself.

Related

SQL check length of a list argument

I have a following query in an Azure SQL database:
Select *
from item_table
where itemNumber IN (:itemNbr) or (:itemNbr) is null;
Means, if the passed list :itemNbr is null or empty then I want all the rows from the table.
The problem I am getting with this query is it runs fine if the :itemNbr is null or empty, but if I pass some objects in the list, then it becomes like this:
Select *
from item_table
where itemNumber IN ('2134234', '23423423') or ('2134234', '23423423') is null;
and I get this error while executing it:
SQL Error [4145] [S0001]: An expression of non-boolean type specified in a context where a condition is expected, near ','
I have tried ISNULL, NULLIF and some other ways but nothing is working correctly
Found one way to handle the same in the query itself, without changing the application code:
Select * from item_table where (COALESCE(:itemNbr,1) = 1 or itemNumber in ( :itemNbr ));
So Now, if we pass the arg itemNbr as null then it will fetch all the rows from the table or else it will filter the rows basis on the itemNbr argument.

Hive "with" clause syntax

The with syntax is absolutely not cooperating, can not get it to work. Here is a stripped down version of it
set hive.strict.checks.cartesian.product=false;
with
newd as (select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table1),
oldd as ( select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table2
where ds='2022-09-08')
select
(newd.risk_score_hi-oldd.risk_score_hi)/newd.risk_score_hi diff_risk_score_hi,
(newd.risk_score_cur-oldd.risk_score_cur)/newd.risk_score_cur diff_risk_score_cur,
from newd cross join oldd
order by 1 desc
Apache Hive Error
[Statement 2 out of 2] hive error: Error while compiling statement:
FAILED: SemanticException [Error 10004]: Invalid table alias or
column reference 'newd': (possible column names are:
diff_risk_score_hi, diff_risk_score_cur)
I had been following the general form shown here: https://stackoverflow.com/a/47351815/1056563
WITH v_text
AS
(SELECT 1 AS key, 'One' AS value),
v_roman
AS
(SELECT 1 AS key, 'I' AS value)
INSERT OVERWRITE TABLE ramesh_test
SELECT v_text.key, v_text.value, v_roman.value
FROM v_text JOIN v_roman
ON (v_text.key = v_roman.key);
I can not understand what I am missing to get the inline with views to work.
Update My query (the first one on top) works in Presto (but obviously with the set hive.strict.checks.cartesian.product=false; line removed). So hive is really hard to get happy for with clauses apparently. I tried like a dozen different ways of using the aliases.

Use table in order by case when does not work

I am running this query but I get an error
select *
from dog
order by case
when exists(
select 1 from dogfood where dog.dogid = dogfood.dogid)
then '1'
else '0' end;
So 2 tables, dog and dogfood which both have a dogid column. I get this error:
[42703][-206] "DOG.DOGID" is not valid in the context where
it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=4.26.14
[56098][-727] An error occurred during implicit system action type
"2". Information returned for the error includes SQLCODE "-206",
> SQLSTATE "42703" and message tokens "DOG.DOGID"..
> SQLCODE=-727, SQLSTATE=56098, DRIVER=4.26.14
I just want to order the dog by if it has a row in dogfood. A solution would be querying the result in the select clause and refer to it in the order by clause, but I want it in the order by clause for my application. I am curious why this query isn't working, I double checked for syntax errors but I could not find any. Am I missing something obvious? I would expect I could refer to a table in the order by which I queried in the select/from clauses.
See documentation
sort-key-expression
An expression that is not simply a column name or an unsigned integer constant. The query to which ordering is applied must be a
subselect to use this form of sort-key. The sort-key-expression cannot
include a correlated scalar fullselect (SQLSTATE 42703) or a function
with an external action (SQLSTATE 42845).
But since it is not correlated you can use IN with a fullselect
select *
from dog
order by
case when dog.dogid in (select dogid from dogfood)
then '1' else '0' end;

ORA-00998: must name this expression with a column alias

I get that I should add alias with all the columns and I'm doing so but I'm still getting error.
CREATE TABLE MENTIONS AS SELECT
UM.USER_ID AS U_ID,
UM.SCREEN_NAME AS USER_SCREEN_NAME,
UM.MENTION_ID AS M_USER_ID,
(
SELECT
UI.USER_SCREEN_NAME AS MENTIONED_USER
FROM
USER_INFO UI
WHERE
UI.USER_ID = UM.MENTION_ID
AND ROWNUM = 1
)
FROM
USER_MENTION UM
USER_MENTION table
USER_ID SCREEN_NAME MENTION_ID
135846337 irisschrijft 774759032636727300
50117969 Chjulian 13769472
14411827 thenriques45 13769472
26681613 ahenotri 252074645
26681613 ahenotri 9796472
158378782 SpringerOpen 9796472
144241914 Kumarappan 252074645
User_INFO table:
USER_ID USER_SCREEN_NAME
22553325 jasonesummers
23435691 QRJAM false
67421923 inTELEgentMSP
97393397 knauer0x
85303739 MarriageTheorem
3842711 seki
3036414608 Bayes_Rule
838677852 BOLIGATOR
I'm still getting the above mentioned error, what am I doing wrong?
Lookup the Oracle Error Message Manual of the current Oracle version. Here the error is mentioned but without additional information.
In such a case look up the
Oracle Error Message Manual of version 9i
For reasons I don't know a lot of error messages have a description in the 9i manual but not in the manuals of higher versions. 9i is a rather old version so the description may be out of date. But it may contain valuable hints.
ORA-00998 must name this expression with a column alias
Cause: An expression or function was used in a CREATE VIEW statement, but no corresponding column name was specified. When expressions or functions are used in a view, all column names for the view must be explicitly specified in the CREATE VIEW statement.
Action: Enter a column name for each column in the view in parentheses after the view name.
We don't have a view but a a table that was created by a select. And actually the last expression of the select list is an expression without an alias. So try your statement using an alias for the last expression. So try
CREATE TABLE MENTIONS AS SELECT
UM.USER_ID AS U_ID,
UM.SCREEN_NAME AS USER_SCREEN_NAME,
UM.MENTION_ID AS M_USER_ID,
(
SELECT
UI.USER_SCREEN_NAME
FROM
USER_INFO UI
WHERE
UI.USER_ID = UM.MENTION_ID
AND ROWNUM = 1
) AS MENTIONED_USER
FROM
USER_MENTION UM
The column alias in the inner select list is useless and can be removed.
The problem with your query is that each column in the create table needs to have a name. You think you are assigning a name in the sub-select. However, you are not.
The subquery is just returning a value -- not a value with a name. So, the AS MENTIONED_USER in your version does nothing. This is a bit tricky, I guess. One way to think of the scalar subquery is that it is just another expression or function call. Things that happen inside it don't affect the outer query -- except for the value being returned.
The correct syntax is to put the column alias outside the subselect, not inside it:
CREATE TABLE MENTIONS AS
SELECT UM.USER_ID AS U_ID, UM.SCREEN_NAME AS USER_SCREEN_NAME, UM.MENTION_ID AS M_USER_ID,
(SELECT UI.USER_SCREEN_NAME
FROM USER_INFO UI
WHERE UI.USER_ID = UM.MENTION_ID AND ROWNUM = 1
) AS MENTIONED_USER
FROM USER_MENTION UM;

Oracle SQL Cast two different columns to one column?

I am selecting data from two different columns depending on what sort of record it is.
Using a Case statement I check what type of record it is and select the appropriate column to insert.
However the syntax for using CAST is not correct and after checking the Oracle docs, there is no reason why it is not working.
CREATE TABLE TEST123 AS
SELECT DISTINCT
bill.ROW_ID,
bill.ACCNT_TYPE_CD,
CASE
WHEN bill.ACCNT_TYPE_CD = 'TestAccount'
THEN CAST(TO_CHAR(bill.INTEGRATION_ID) AS VARCHAR2(30)) AS NUM
ELSE CAST(TO_CHAR(bill.OU_NUM )AS VARCHAR2(30)) AS NUM
END
FROM SIEBEL.S_ORG_EXT bill
INNER JOIN Products prod
ON prod.BILL_ACCNT_ID = bill.ROW_ID
OR prod.CUST_ACCNT_ID = bill.ROW_ID;
Error code is:
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
AS NUM must follow the END of the CASE. If you could set the column name for each condition separately, then the column could have two different names. Clearly that can't work.
CASE
WHEN bill.ACCNT_TYPE_CD = 'TestAccount'
THEN CAST(TO_CHAR(bill.INTEGRATION_ID) AS VARCHAR2(30))
ELSE CAST(TO_CHAR(bill.OU_NUM )AS VARCHAR2(30))
END AS NUM
Also, using both TO_CHAR and CAST(... as VARCHAR(30)) is redundant. They are both ways of converting a non-string to a string.
And since CASE .. END is a function, you can go even further than explained by Allan:
SELECT DISTINCT ...
CAST(
CASE WHEN bill.ACCNT_TYPE_CD = 'TestAccount'
THEN bill.INTEGRATION_ID
ELSE bill.OU_NUM
END
AS VARCHAR(30) -- `AS` here is part of the `CAST` function
-- and will denote the requested data type
) AS NUM -- `AS` here introduces the column alias
If your not familiar with the CAST function, please beware of the double meaning of the AS keyword here. See the comments in the code above