ORA-00900 error in dbSendStatement() of 'RJDBC' - r

I am trying to upload data from R to database, I used package 'RJDBC' to connect R to Oracle. The connection is set up and I am able to load data from Oracle to R. When I tried to insert data into a table in Oracle using dbSendStatement():
sqlQuery_uploadResult<-function(A,B,C,D,E,F,G,H,I){
sprintf("INSERT INTO DEMAND_FCAST (A,B,C,D,E,F,G,H,I) VALUES (TO_DATE('%s','DD-MON-YY hh24:mi:ss'),%1.4f,%5.2f,%2.0f,%1.4f,%5.2f,%2.0f,%2.0f)",A,B,C,D,E,F,G,H,I)
}
dbSendStatement(conn,sqlQuery_uploadResult(A,B,C,D,E,F,G,H,I))
I got an error message:
Error in .verify.JDBC.result(md, "Unable to retrieve JDBC result set meta data for ", :
Unable to retrieve JDBC result set meta data for INSERT INTO DEMAND_FCAST (A,B,C,D,E,F,G,H,I) VALUES (TO_DATE('17-Oct-16 13:35:45','DD-MON-YY hh24:mi:ss'),0.1160,700,36,0.4037,965,35, 1) in dbSendQuery (ORA-00900: invalid SQL statement
)
However, I checked the table in Oracle, the record I tried to insert is in the table, is there anyone has any clue why does this error turn out and how to deal with this?

I was having the same issue. I tried dbExecute() and dbSendQuery() and got a similar error. A colleague suggested that I use dbSendUpdate() and it executed without error.

Related

How to receive a success message from Teradata to R?

first time poster here. I’m using R to ‘automate’ some Teradata (TD) SQL scripts. The ones that return data are working great. But I have an UPDATE SQL statement that only returns a message in TD like ‘xxx rows updated’. I’m using the RODBC package in R for my connection. When I use ‘sqlQuery’ and send the ‘update’ SQL statement to TD from R, I get nothing back, whether successful or not. I know only data returning is normal here, but what I want is to get that message back from TD to R. Then I can continue to ‘automate’ things based on the message. Is there a way, either in putting something in the SQL code at the end, or afterwards with R, to get this ‘successful’ message back?
Package RODBC has “odbcGetErrMsg”, but it doesn’t work on the success message. The only workaround I can think of is to do a count() before the update, then send the update statement, then count() after to get a number of rows changed. This may work, but I’d like to get the message instead. I’ve searched SO & Googled this with no luck. Any ideas of how to get a successful TD message from an update statement sent from R returned back in R please?

MariaDB: SELECT INSERT from ODBC CONNECT engine from SQL Server keeps causing "error code 1406 data too long"

Objective: Using MariaDB I want to read some data from MS SQL Server (via ODBC Connect engine) and SELECT INSERT it into a local table.
Issue: I keep geting "error code 1406 data too long" even if source and destination varchar fields have the very same size (see further details)
Details:
The query which I'm trying to execute is in the form:
INSERT INTO DEST_TABLE(NUMERO_DOCUMENTO)
SELECT SUBSTR(TRIM(NUMERO_DOCUMENTO),0,5)
FROM CONNECT_SRC_TABLE
The above is the very minimal subset of fields which causes the problem.
The source CONNECT Table is actually a view inside SQL Server. The destination table has been defined so to be identical to the the ODBC CONNECT Table (same field names, same NULL constranints, same filed types ans sizes)
There's no issue on a couple of other VARCHAR fields
The issue is happening with a filed NUMERO_DOCUMENTO VARCHAR(14) DEFAULT NULL where the max length from the input table is 14
The same issue is also happening with 2 other fields ont the same table
All in all it seems to be an issue with the source data rather then the destination table.
Attemped workarounds:
I tried to force silent truncation but, reasonably, this does not make any difference: Error Code: 1406. Data too long for column - MySQL
I tried enlarging the destination field with no appreciable effect NUMERO_DOCUMENTO VARCHAR(100) DEFAULT NULL
I tried to TRIM the source field (hidden spaces?) and to limit its size at source to no avail: INSERT INTO DEST_TABLE(NUMERO_DOCUMENTO) SELECT SUBSTR(TRIM(NUMERO_DOCUMENTO),0,5) FROM CONNECT_SRC_TABLE but the very same error is always returned
Workaround:
I tried performing the same thing using a FOR x IN (src_query) DO INSERT .... END FOR and this solution seems to work: this means that the problem is not into the data itself but in how the engine performs the INSERT SELECT query

How to update a SQL table using RJDBC in R

I already have a SQL table and I want to update the table on a daily basis. I want to overwrite the table everyday.
If I use dbWriteTable() command it is giving me an error "ORA-00955: name is already used by an existing object".
How can I get rid of this error?
Can I use dbSendUpdate() here? If yes,how?

Error in dbListFields(con, VisitDetails) : object 'VisitDetails' not found

I am using sql server database in R. And trying to build a data frame with col1, col2. But while raise a query for list of fields in table "VisitDetails" it throwing error.
dbListFields(con, VisitDetails)
Can anyone tell me why I am not able to list the fields of table?
Where did I wrong for the above syntax?
"con" is the connection to sql server with login.
Thanks in advance.
I tried with dbListFields(con, "VisitDetails").
It works.

R Redshift dbExistTable dbWriteTable

I am having issues working with the schemas in Redshift connecting using R.
url <- "jdbc:url:port/database?user=X123&password=fakepassword"
conn <- dbConnect(driver, url)
so I connect fine, and when I list tables I notice the default schema is public, but I don't want to work with that schema, how do I switch schemas ?
say if it is: lab_space
when I try this, it still lists tables in public:
dbListTables(conn, schema='lab_space')
tried this and I get an error:
SET search_path = lab_space;
> SET search_path = 'cust_usr';
Error: unexpected symbol in "SET search_path"
I must be doing something wrong ?
When I try to say check for a table and delete:
droptable <- dbSendQuery(conn, "drop table if exists lab_space.Tablebla")
it will drop it, but still give me an error:
Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ", :
Unable to retrieve JDBC result set for drop table if exists lab_space.Tablebla ([JDBC Driver]com.amazon.dsi.dataengine.impl.DSISimpleRowCountResult cannot be cast to com.amazon.dsi.dataengine.interfaces.IResultSet)
You can change the schema using dbSendUpdate to change the search_path for the connection:
dbSendUpdate(jdbc_con,"set search_path to my_schema")
ok, on this one I went with this driver
drv <- dbDriver("PostgreSQL")
and then things worked with no stupid warnings errors, I guess R and Redshift still has a lot of growing up to do, not sure :- )
In redshift, you can set the search path with set search_path to <schema_name> (no equals)
dbSendQuery(con,"set search_path to <schema_name>");
You may assign dbSendQuery() result to var if underlying sql returns a resultset. Here drop query doesn't return any result set Object. Try without assigning to any var.
dbSendQuery(conn, "drop table if exists lab_space.Tablebla")

Resources