MariaDB: subselect in COALESCE throws ERROR 1064 (42000) - sql

Intention: I want to subselect values within a coalesce function in MariaDB (10.7.1).
Problem:
Executing this SQL statement ...
select coalesce(select id from MY_TABLE limit 1);
... throws this error message:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'select id from MY_TABLE limit 1)' at line 1
Workarounds / checks I tried:
select id from MY_TABLE limit 1; returns a valid value. So that part of the statement is fine.
select coalesce('a'); returns the value a. So executing coalesce with one parameter only is also fine.

You must make the result of select to be an expression. Statement is not an expression
with MY_TABLE as (
select 1 as id
)
select coalesce((select id from MY_TABLE limit 1)) c;
Note the result of LIMIT without ORDER BY is an arbitrary row.

The COALESCE function typically takes 2 or more arguments. The value returned is the first value which is not NULL. You probably intend something like this:
SELECT COALESCE(id, 'missing') AS id
FROM MY_TABLE
ORDER BY <some column>
LIMIT 1;

Related

USING CAST STATEMENT IN INSERT INTO STATEMENT SQL

Below (1) is the query which I wrote and when I am not using CAST i am getting error (2) and when am using cast its throwing another syntax error(3)
(1)
/*DROP TABLE IF EXISTS LFB1_BACKUPTABLE
CREATE TABLE LFB1_BACKUPTABLE AS
(SELECT * FROM LFB1);
*/
INSERT INTO LFB1_BACKUPTABLE (
MANDT,LIFNR,BUKRS,PERNR,ERDAT,ERNAM,SPERR,LOEVM,ZUAWA,
AKONT,BEGRU,VZSKZ,ZWELS,XVERR,ZAHLS,ZTERM,EIKTO,ZSABE,
KVERM,FDGRV,BUSAB,LNRZE,LNRZB,ZINDT,ZINRT,DATLZ,XDEZV,
WEBTR,KULTG,REPRF,TOGRU,HBKID,XPORE,QSZNR,QSZDT,QSSKZ,
BLNKZ,MINDK,ALTKN,ZGRUP,MGRUP,UZAWE,QSREC,QSBGR,QLAND,
XEDIP,FRGRP,TOGRR,TLFXS,INTAD,XLFZB,GUZTE,GRICD,GRIDT,
XAUSZ,CERDT,CONFS,UPDAT,UPTIM,NODEL,TLFNS,AVSND,AD_HASH,
J_SC_SUBCONTYPE,J_SC_COMPDATE,J_SC_OFFSM,J_SC_OFFSR,
BASIS_PNT,GMVKZK,PREPAY_RELEVANT,ASSIGN_TEST, CAST(_CELONIS_CHANGE_DATE AS DATE) AS _CELONIS_CHANGE_DATE
)
SELECT DISTINCT * FROM LFB1
WHERE MANDT||LIFNR||BUKRS NOT IN (SELECT DISTINCT MANDT||LIFNR||BUKRS FROM
LFB1_BACKUPTABLE);
(2) Execution error:
[Vertica]VJDBC ERROR: Column "_CELONIS_CHANGE_DATE" is of type timestamptz but expression is of type varchar
(3) Execution error:
[Vertica]VJDBC ERROR: Syntax error at or near "CAST"
Why are you using a cast statement in your insert table column list? You can't change the type of the destination table columns.
You need to change the type of the values you are inserting.
INSERT INTO LFB1_BACKUPTABLE (
MANDT,LIFNR,BUKRS,PERNR,ERDAT,ERNAM,SPERR,LOEVM,ZUAWA,
AKONT,BEGRU,VZSKZ,ZWELS,XVERR,ZAHLS,ZTERM,EIKTO,ZSABE,
KVERM,FDGRV,BUSAB,LNRZE,LNRZB,ZINDT,ZINRT,DATLZ,XDEZV,
WEBTR,KULTG,REPRF,TOGRU,HBKID,XPORE,QSZNR,QSZDT,QSSKZ,
BLNKZ,MINDK,ALTKN,ZGRUP,MGRUP,UZAWE,QSREC,QSBGR,QLAND,
XEDIP,FRGRP,TOGRR,TLFXS,INTAD,XLFZB,GUZTE,GRICD,GRIDT,
XAUSZ,CERDT,CONFS,UPDAT,UPTIM,NODEL,TLFNS,AVSND,AD_HASH,
J_SC_SUBCONTYPE,J_SC_COMPDATE,J_SC_OFFSM,J_SC_OFFSR,
BASIS_PNT,GMVKZK,PREPAY_RELEVANT,ASSIGN_TEST, _CELONIS_CHANGE_DATE
)
SELECT DISTINCT col1, col2, col3, ..., timestamp_col::DATE FROM LFB1
WHERE MANDT||LIFNR||BUKRS NOT IN (SELECT DISTINCT MANDT||LIFNR||BUKRS FROM LFB1_BACKUPTABLE);
Your syntax for casting to a date is syntactically correct, but in this example I used the more common Vertica syntax which is col::DATE, both will work.

How to save query result when using SQL in R?

If just SQL, we can use the following code to save a query result in a temporary table, so that we can use the result later.
CREATE TABLE #TEMPTABLE
(
Column1 type1,
Column2 type2,
Column3 type3
)
INSERT INTO #TEMPTABLE
SELECT ...
SELECT *
FROM #TEMPTABLE ...
This answer is from How to save select query results within temporary table?
However, I am using R to connect HANA. I need to use SQL query in R to select data from HANA. I need a tempory table for my query result. My code is like this:
sqlQuery(ch,paste('
CREATE TABLE #myTemp(
"/BIC/ZSALE_OFF" INT
)
INSERT INTO #myTemp
SELECT
"/BIC/ZSALE_OFF"
FROM
"SAPB1D"."/BIC/AZ_RT_A212"
'))
I have got the following error information:
[1] "42000 257 [SAP AG][LIBODBCHDB DLL][HDBODBC] Syntax error or access violation;257 sql syntax error: incorrect syntax near \"INSERT\": line 5 col 19 (at pos 124)"
[2] "[RODBC] ERROR: Could not SQLExecDirect '\n CREATE TABLE #myTemp(\n \"/BIC/ZSALE_OFF\" INT\n )\n INSERT INTO #myTemp\n SELECT\n \"/BIC/ZSALE_OFF\"\n FROM\n \"SAPB1D\".\"/BIC/AZ_RT_A212\"\n
Without the temperory result part, the code for just query is correct:
sqlQuery(ch,paste('
SELECT
"/BIC/ZSALE_OFF"
FROM
"SAPB1D"."/BIC/AZ_RT_A212"
'))
I am not sure if the grammar is correct, or there is something else I do not understand.

How to retrieve data from Table-Function in DB2

I have a Table Function it return a table of (student_id,student_name)
I want to call it and insert the result into another table
I use
INSERT INTO STUDENT_TMP SELECT Table(MyDB.fn_getStudent())
but i did not get the result
I have got an error :
ERROR: DB2 SQL Error: SQLCODE=-390, SQLSTATE=42887,
SQLERRMC=MyDB.AA;SQL131208155041300,DRIVER=3.67.26
Error Code: -390
I found the followin exsample on ibm sites:
select t1.timeid, t1.storeid, t1.sales
from time, store, table (cvsample.salesfunc(time.timeid, store.storeid)) as t1
where time.timeid = t1.timeid and store.storeid = t1.storeid;
notice the syntax: table (cvsample.salesfunc(time.timeid, store.storeid)) as t1
so you prob dont need fields and 'as' you still need '*' and the 'FROM'
so
INSERT INTO STUDENT_TMP SELECT * FROM Table (MyDB.fn_getStudent())

Oracle ProC INSERT INTO VALUES ( (select ...) )

Running Pro*C on Oracle 10g.
I am looking to do a subquery within an insert statement values clause. This sql query is fully valid and runs within TOAD with no problems, but Pro*C fails to parse the query.
EXEC SQL INSERT INTO TARGET_ATTACHMENT
(
TARGET_ID
FILENAME
)
VALUES (
:targetID,
( SELECT CREATED_FLAG from TARGET t where t.TARGET_ID = :targetID ) || '.tif'
)
If I remove:
( SELECT (CREATED_FLAG || DISPLAY_ID) from TARGET t where t.TARGET_ID = :targetID ) ||**".
The Pro*C compiler works and everything compiles and runs as expected.
If I DO NOT remove:
The Pro*C compiler throws a syntax error.
1>Syntax error at line 128, column 12, file d:\SVN\...\TA.pc:
1>Error at line 128, column 12 in file d:\SVN\...
1>...\TA.pc
1> ( select CREATED_FLAG from target t where t.TARGET_ID = :targetID )
1>...........1
1>PCC-S-02201, Encountered the symbol "CREATED_FLAG" when expecting one of the fol
1>lowing:
1> ( ) * + - / . # | at, day, hour, minute, month, second, year,
This is a problem, as I expect Pro*C to be able to compile subquerys within a values caluse:
ie.
INSERT into table1 (col1) values ( (select t2.singleCol from table2 t2 where t2.priKey = :priKey) )
Is this expected behaviour of Pro*C? or Should it support subqueries within the values clause?
Possibly change the subquery to:
( SELECT CREATED_FLAG || '.tif' from TARGET t where t.TARGET_ID = :targetID )
I dont think I have ever seen something appended to a subquery the way you were attempting.
The amount of SQL the Pro*C preprocessor is able to parse in static SQL statements is quite limited. For example it can't even parse explicit inner joiner/outer left join etc. notation.
As a workaround you can just prepare a dynamic SQL statement and execute it - even if your SQL statement is not really dynamic.
The code you have posted is logically identical to this:
INSERT INTO TARGET_ATTACHMENT
( TARGET_ID , FILENAME )
select :targetID, CREATED_FLAG|| '.tif'
from TARGET t
where t.TARGET_ID = :targetID )
Is there a particular reason why you need to use scalar cursors in a VALUES clause?

How can I update a record using a correlated subquery?

I have a function that accepts one parameter and returns a table/resultset. I want to set a field in a table to the first result of that recordset, passing in one of the table's other fields as the parameter. If that's too complicated in words, the query looks something like this:
UPDATE myTable
SET myField = (SELECT TOP 1 myFunctionField
FROM fn_doSomething(myOtherField)
WHERE someCondition = 'something')
WHERE someOtherCondition = 'somethingElse'
In this example, myField and myOtherField are fields in myTable, and myFunctionField is a field return by fn_doSomething. This seems logical to me, but I'm getting the following strange error:
'myOtherField' is not a recognized OPTIMIZER LOCK HINTS option.
Any idea what I'm doing wrong, and how I can accomplish this?
UPDATE:
Based on Anil Soman's answer, I realized that the function is expecting a string parameter and the field being passed is an integer. I'm not sure if this should be a problem as an explicit call to the function using an integer value works - e.g. fn_doSomething(12345) seems to automatically cast the number to an string. However, I tried to do an explicit cast:
UPDATE myTable
SET myField = (SELECT TOP 1 myFunctionField
FROM fn_doSomething(CAST(myOtherField AS varchar(1000)))
WHERE someCondition = 'something')
WHERE someOtherCondition = 'somethingElse'
Now I'm getting the following error:
Line 5: Incorrect syntax near '('.
I have never done anything like this so .... all the code I have seen uses a schema on the function name - so something like:
FROM dbo.fn_doSomething(myOtherField)
seems like a compiler bug in SQL 2000
try in Server 2005 and join the table-valued function using CROSS APPLY or OUTER APPLY
also try this, guru huys
CREATE FUNCTION FCN_pruebaChicaBorrame(#numerito int)
RETURNS #returnTable TABLE (numerito int)
AS
BEGIN
insert into #returnTable values(#numerito)
return
END
Select * from FCN_pruebaChicaBorrame(20)
Select col_1
from ( select 1 as col_1
union select 2
union select 3) as tablita
Select col_1, (select * from dbo.FCN_pruebaChicaBorrame(20) as fcnTable)
from ( select 1 as col_1
union select 2
union select 3) as tablita
Select col_1, (select * from dbo.FCN_pruebaChicaBorrame(col_1) as fcnTable)
from ( select 1 as col_1
union select 2
union select 3) as tablita
Select col_1, (select * from dbo.FCN_pruebaChicaBorrame(case when 1=1 then 20 else 21) as fcnTable)
from ( select 1 as col_1
union select 2
union select 3) as tablita
I searched on google for this error and one person talks about missing single quotes in search condition. Is that the case with your function code? link to related blog
It seems that (at least in SQL Server 2000) you can't pass a column value to a table valued function. I had to set up a scalar function to get around this.