2

I have a synonym statement:

CREATE OR REPLACE SYNONYM  sample-table-name FOR master.sample-table-name

I want to execute this by using R language using RJDBC library. I've tried different functions including dbGetQuery function etc. But it is not able to execute this statement.

Could someone please advise on how to execute these statements in R.

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

2

I would expect that using dbExecute() should work. Failing that, dbSendStatement() should almost certainly work, as it seems to be intended for that sort of DDL.

The docs on dbGetQuery say:

This method is for SELECT queries only

To look at the functions that are available to you, it can be helpful to do something like ls('package:DBI'). Per the docs:

dbExecute:

Executes a statement and returns the number of rows affected. dbExecute() comes with a default implementation (which should work with most backends) that calls dbSendStatement(), then dbGetRowsAffected(), ensuring that the result is always free-d by dbClearResult().

dbSendStatement:

The dbSendStatement() method only submits and synchronously executes the SQL data manipulation statement (e.g., UPDATE, DELETE, INSERT INTO, DROP TABLE, ...) to the database engine. To query the number of affected rows, call dbGetRowsAffected() on the returned result object. You must also call dbClearResult() after that. For interactive use, you should almost always prefer dbExecute().

EDIT: Those methods will likely work for other implementations of DBI, and perhaps for RJDBC in the future. In any case, it looks like they have implemented dbSendStatement() using dbSendQuery(), so they will not work for this purpose.

In the RJDBC framework, dbSendUpdate() is what you want.

dbSendQuery and dbSendUpdate submit a SQL query to the database. The difference between the two is only that dbSendUpdate is used with DBML queries and thus doesn't return any result set.

Confirmed using a similar query on another DBMS:

dbSendStatement(conn,'SET search_path=public;')
#Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ",  : 
#Unable to retrieve JDBC result set for SET search_path=public; (No results were returned by the query.)

traceback()
#7: stop(..., " (", .jcall(x, "S", "getMessage"), ")")
#6: .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ", 
#       statement)
#5: .local(conn, statement, ...)
#4: dbSendQuery(conn, statement, ...)
#3: dbSendQuery(conn, statement, ...)                ## this is the problem
#2: dbSendStatement(conn, "SET search_path=public;")
#1: dbSendStatement(conn, "SET search_path=public;")

dbSendUpdate(conn, 'SET search_path=public;')
# successful

dbCommit(conn)
# persist the change past the current session (RJDBC seems to give you transaction control here)
cole
  • 1,737
  • 2
  • 15
  • 21
  • dbExecute or dbSendStatement didn't work. Any other working alternative. These grants and synonyms come under the oracle objects. So to execute these objects there should be some other function in R – Susarla Nikhilesh Jun 28 '17 at 12:16
  • Any info on the error messages you received when they did not work? – cole Jun 29 '17 at 02:59
  • Error in .verify.JDBC.result(md, "Unable to retrieve JDBC result set meta data for ", : Unable to retrieve JDBC result set meta data for CREATE OR REPLACE SYNONYM sample-table-name FOR master.sample-table- name in dbSendQuery (ORA-01003: no statement parsed The executed statement is : outputrecord<-dbExecute(jdbcConnection,runquery); But even then in the error it is showing dbSendQuery.. – Susarla Nikhilesh Jun 29 '17 at 04:06
  • Ok, I am not super familiar with `RJDBC`, but it looks like they may have implemented `dbSendUpdate()` - have you tried that yet? – cole Jun 29 '17 at 09:53
  • DbSendUpdate didn't throw any error but the return value was NULL and the operation didn't happen. – Susarla Nikhilesh Jun 29 '17 at 10:55
  • This may be silly... I don't think you need a semicolon, but it couldn't hurt. Other options: (1) Try some other database statements (CREATE TABLE, etc.). (2) Try executing `SELECT` statements within the same session (from R), to see if the DBML is being executed for the session but not persisted. (3) Try executing `dbCommit` afterwards. Also, we expect a NULL return value. – cole Jun 30 '17 at 01:30
  • Yes dbCommit helped it. Thanks a lot. @C.Arendt – Susarla Nikhilesh Jun 30 '17 at 07:22