dashDB: An unexpected token "IF" was found following " ". Expected tokens may include: "<call>". SQLSTATE=42601 - database

I am trying to pass following query to dashdb:
IF (NOT EXISTS (SELECT * FROM TEST_CONFIGURATION WHERE DEVICEID = 'OZ_POLLUDRON_010'))
BEGIN
INSERT INTO TEST_CONFIGURATION (DEVICEID, DEVICETYPE, SERIAL, TYPE, MACID, CONFIG, INIT) VALUES('OZ_POLLUDRON_010', 'POLLUDRON_PRO', '9428424248', 0, '200050000b51343334363138', '[object Object]', '[object Object]')
END
ELSE
BEGIN
UPDATE TEST_CONFIGURATION SET DEVICEID = 'OZ_POLLUDRON_010', DEVICETYPE = 'POLLUDRON_PRO', SERIAL = '9428424248', TYPE = 0, MACID = '200050000b51343334363138', CONFIG = '[object Object]', INIT = '[object Object]' WHERE DEVICEID = 'OZ_POLLUDRON_010' END
But it is giving me following error:
database/sql/driver: [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "IF" was found following " ". Expected tokens may include: "<call>". SQLSTATE=42601
Can anyone please help me out with it?

Consider using the MERGE statement to achieve an upsert action (insert if not exist else update). Examples are in the DB2 samples directory and in the DB2 Knowledge Center.

Related

#Teradata error expected something like ')' between ';' and the end of the request

REPLACE MACRO GEO_MATCHED (REGION INTEGER) AS (
INSERT INTO DLA_v.GEO_FULL_2019_MATCHED
SELECT
Poly.Project_ID AS "Project_ID",
Poly.WIKIMAPIA_ID AS "POLY_ID_WIKIMAPIA",
Poly.NAME AS "POLY_NAME",
Fast.REVENUE_YTD,
Fast.JOB_VERTICAL,
Poly.geometry.ST_Contains(Fast.geometry) AS "INSIDE"
FROM
(SELECT * FROM DLA_A_19143.geo_jobsite_input_data
WHERE region__c = :REGION)Fast, DLA_D_SLSMKT.GEO_POLYGONS Poly
WHERE
(Poly.geometry.ST_Contains(Fast.geometry) = 1;);
----region with null values
getting this error for this code :
[42000] [Teradata][ODBC Teradata Driver][Teradata Database] Syntax error, expected something like ')' between ';' and the end of the request.
Added the brackets as it has asked to add the bracted between semicolumns and end of request its not working, if any one could help .

How do I work with variables in snowflake

I set this up a while ago in something that is not really used. I was under the impression that it worked at the time, but I'm trying to test it now and am getting some errors.
Here is my snowflake code:
var rs = snowflake.createStatement( { sqlText: "select count(*) from toptal.stage_resellers" } ).execute();
rs.next();
var resellers = rs.getColumnValue(1);
I that that var resellers was going to define a variable that would have the number of rows in stage_resellers, but I'm not confident of that.
I decided to test it just by inserting the value into a logging table that I am using into an unused column for the time being.
Here is that code:
var stmt1 = snowflake.execute ( { sqlText:`insert into toptal.processing_executions values ('merge into dim_resellers', current_timestamp, 'processing', :resellers);`});
I'm getting this error:
Execution error in store procedure PROCESSING: SQL compilation error: error line 1 at position 110 Bind variable :resellers not set. At Snowflake.execute, line 47 position 24
I tried futzing around with setting the variable, to no avail.
I have a feeling that I am mixing up environments here, but I'm not sure what's going on.
To execute:
var stmt1 = snowflake.execute ( { sqlText:`insert into toptal.processing_executions values ('merge into dim_resellers', current_timestamp, 'processing', :resellers);`});
variables has to be provided as ? or :1. Code becomes:
var stmt1 = snowflake.execute({
sqlText: `insert into toptal.processing_executions
values ('merge into dim_resellers', current_timestamp,
'processing', ?)`
,binds: [resellers] } );
More at documentation: Binding Variables

fetchall error in PDO

I'm trying to write code to insert new items into a database.
if(isset($_POST['btnAddVeg'])){
$addVegType = $_POST['txtAddVegType'];
$AddVegName = $_POST['txtAddVegName'];
$insertQuery = 'insert into vegetables (vegetable_type, vegetable_id) values ("$addVegType", "$AddVegName")';
$statement2 = $db->prepare($insertQuery);
$statement2->execute();
$results2 = $statement2->fetchAll();
$statement2->closeCursor();
foreach($results2 as $result2){
echo "<table border = '0'><th>ID</th><th>Type</th><th>Name</th> <tr><td>".$result2['vegetable_id'].'</td>';
echo "<td>".$result2['vegetable_type'].'</td>';
echo "<td>".$result2['vegetable_name'].'</td></tr></table>';
}
I get this error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error in /vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/task9.php(54): PDOStatement->fetchAll() #1 {main} thrown in vvvvvvvvvvv/task9.php on line 54
Probably ridiculously easy but what am I missing?
Thank you
Executing an INSERT statement doesn't return a resultset object.
Just remove the line that does the statement2->fetchAll.
And remove the lines that reference $results2.
We would need that if we were executing a SELECT statement, to get a resultset (a set of rows) returned.
Also important to point out: the code is vulnerable to SQL Injection.
https://xkcd.com/327/
https://www.owasp.org/index.php/SQL_Injection

PreparedStatement not populating

I have PreparedStatement(ps) below, populate it as shown and when executed MSSQL errors as shown last below. Of course there is NO value of "TRADEIN" in any data that can be seen. Ideas?
String update = "UPDATE IM_ITEM SET "
+ "LST_COST=?, PRC_1=?, IS_TXBL=?, TAX_CATEG_COD=?,CATEG_COD=?"
+ " WHERE ITEM_NO="+itemNo;
populate:
ps.setString(++i, q.get("LST_COST"));
ps.setString(++i, q.get("PRC_1"));
ps.setString(++i, q.get("IS_TXBL"));
ps.setString(++i, q.get("TAX_CATEG_COD"));
ps.setString(++i, q.get("CATEG_COD"));
debugged: "sqlCommand" and "userSQL" are still parameterized only
UPDATE IM_ITEM SET LST_COST=?, PRC_1=?, IS_TXBL=?, TAX_CATEG_COD=?, CATEG_COD=? WHERE ITEM_NO=101316
error:
Severe: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value 'TRADEIN' to data type int.
Answered, kindly, by Seanlange in the chat.

Saving DataSet<Row> as CSV

Is it possible to save the DataSet<Row> as CSV output? toDataSet API expects Row.class and writeAsCSV expects Tuple. Could you please help?
DataSet<Row> finalResult = tEnv.toDataSet(flattenedTripByGender, Row.class);
finalResult.writeAsCsv("../data/tripbygender.csv");
And this is an error:
Caused by: java.lang.ClassCastException: org.apache.flink.api.table.Row cannot be cast to org.apache.flink.api.java.tuple.Tuple at org.apache.flink.api.java.io.CsvOutputFormat.writeRecord(CsvOutputFormat.java:44)
Aruna
You can directly emit a Table without converting it to a DataSet using a TableSink.
Flink 1.1.x comes with a CsvTableSink which is used as follow:
// compute your result
Table result = // ...
// create a TableSink
TableSink sink = new CsvTableSink("/path/to/file", fieldDelim = "|");
// write the result Table to the TableSink
result.writeToSink(sink);

Resources