RODBC Error for CURRENT_TIMESTAMP() - is there a list of acceptable keywords? - r

I submitted a simple (so I thought) query via RODBC :
ch <- odbcConnect(dsn.name, believeNRows=FALSE, rows_at_time=1)
sqlQuery(ch, "CURRENT_TIMESTAMP()")
And it threw the following error:
[1] "42000? -1 Malformed SQL Statement: Unrecognized keyword: CURRENT_TIMESTAMP\r\nStatement:CURRENT_TIMESTAMP()"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'CURRENT_TIMESTAMP()'"
I thought CURRENT_TIMESTAMP() is a common SQL command and didn't expect this to not run. I had checked that the ODBC connection (RSSBus DynamicsCRM Source x64) supports CURRENT_TIMESTAMP(). My connection is OK, I was able to perform some other SQL queries.
So is there a problem with my syntax above? Or is there a list of keywords that RODBC doesn't recognise?

In the above code, first line i.e.
ch <- odbcConnect(dsn.name, believeNRows=FALSE, rows_at_time=1)
creates a connection to your ODBC data source name (dsn.name). So here ch basically stores the connection instance. The second line:
sqlQuery(ch, "CURRENT_TIMESTAMP()")
executes the SQL query on the connection i.e. ch and returns the result in a data frame. So instead of using the method CURRENT_TIMESTAMP() use complete query:
sqlQuery(ch, "SELECT CURRENT_TIMESTAMP()")
I hope this will help.

Related

SQR - how to use FROM [dynamic table name} within BEGIN-SELECT?

I have to create an SQR that generates a list of EEIDs, if there were any changes to the Pension data in the past day. The SQR compiles and works perfectly when I hardcode in the table names.
However, when I tried using variables for the table names, I get a compile error
I've pasted the portion of SQR that I'm trying to fix
When I start using $tableName and $auditTableName as table variables, that's when I get the error and I'm not sure what is going wrong
Can anyone help?
Please and Thank You
!***************************
begin-procedure Process-Main
!***************************
let $tableName = 'PS_PENSION_PLAN'
let $auditTableName = 'PS_AUDIT_PENSION_PLN'
let $dummy-dyn-variable = ''
begin-SELECT DISTINCT
L.EMPLID
L.EMPL_RCD
do someProcName(&L.EMPLID, &L.EMPL_RCD)
FROM [$dummy-dyn-variable]
(
SELECT DISTINCT
PP.EMPLID,
PP.EMPL_RCD,
PP.EFFDT,
'1901-01-01 12:00:00' AS AUDIT_STAMP
FROM [$dummy-dyn-variable] [$tableName] PP
UNION
SELECT DISTINCT
A.EMPLID,
A.EMPL_RCD,
A.EFFDT,
A.AUDIT_STAMP
FROM [$dummy-dyn-variable] [$auditTableName] A
)L
WHERE DATEDIFF(DAY,CAST(L.AUDIT_STAMP AS DATE),SYSDATE) = 1
ORDER BY 1,2
end-SELECT
end-procedure
Edit:
does the UNION have anything to do with this?
I keep receiving is this error:
(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 102 in cursor 1:
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'FROM'.
(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 8180 in cursor 1:
[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.
Edit2:
Ok, initial problem solved with [$dummy-dyn-variable], which led to the next problem with the DO command. I've updated the code above with DO someProcName(param_a, param_b)
I am now getting an error saying:
(SQR 2002) DO arguments do not match procedure's
Weird part, if I remove the dynamic table variables and hardcode the table names in the FROM section, then it compiles properly without errors. This makes me believe that the error is not related to my someProcName (maybe?)
am I missing something here?

Datastage job failed netezza to greenplum data load using ODBC Greenplum Wire Protocol driver

Greenplum_Connector_0,0: The following SQL statement failed: INSERT INTO GPCC_TT_20211121154035261_15420_0_XXXXX_TABLE_NAME (COLUMN1,COLUMN2,...) SELECT COLUMN1,COLUMN2,... FROM GPCC_ET_20211121154035417_15420_0. The statement reported the following reason: [SQLCODE=HY000][Native=3,484,948] [IBM (DataDirect OEM)][ODBC Greenplum Wire Protocol driver][Greenplum]ERROR: missing data for column "xyz_id" (seg2 slice1 192.168.0.0:00 pid=30826)(Where External table gpcc_et_20211121154035417_15420_0, line 91 of gpfdist://ABCD:123/DDCETLMIG_15420_gpw_3_3_20211121154035261: "AG?199645?ABCD EFGH. - HELLOU - JSF RT ADF?MMM?+1?A?DAD. SDA?0082323209?N?N..."; File copy.c; Line 5211; Routine NextCopyFromX; )
The trick here is to read the error message carefully. Somehow your job has managed not to provide a value for column xyz_id. Check your job design thoroughly.

Create a stored procedure using RMySQL

Background: I am developing a rscript that pulls data from a mysql database, performs a logistic regression and then inserts the predictions back into the database. I want the entire system to be self contained in the script in case of database failure. This includes all mysql stored procedures that the script depends on to aggregate the data on the backend since these would be deleted in such a database failure.
Question: I'm having trouble creating a stored procedure from an R script. I am running the following:
mySQLDriver <- dbDriver("MySQL")
connect <- dbConnect(mySQLDriver, group = connection)
query <-
"
DROP PROCEDURE IF EXISTS Test.Tester;
DELIMITER //
CREATE PROCEDURE Test.Tester()
BEGIN
/***DO DATA AGGREGATION***/
END //
DELIMITER ;
"
sendQuery <- dbSendQuery(connect, query)
dbClearResult(dbListResults(connect)[[1]])
dbDisconnect(connect)
I however get the following error that seems to involve the DELIMITER change.
Error in .local(conn, statement, ...) :
could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
CREATE PROCEDURE Test.Tester()
BEGIN
/***DO DATA AGGREGATION***/
EN' at line 2
What I've Done: I have spent quite a bit of time searching for the answer, but have come up with nothing. What am I missing?
Just wanted to follow up on this string of comments. Thank you for your thoughts on this issue. I have a couple Python scripts that need to have this functionality and I began researching the same topic for Python. I found this question that indicates the answer. The question states:
"The DELIMITER command is a MySQL shell client builtin, and it's recognized only by that program (and MySQL Query Browser). It's not necessary to use DELIMITER if you execute SQL statements directly through an API.
The purpose of DELIMITER is to help you avoid ambiguity about the termination of the CREATE FUNCTION statement, when the statement itself can contain semicolon characters. This is important in the shell client, where by default a semicolon terminates an SQL statement. You need to set the statement terminator to some other character in order to submit the body of a function (or trigger or procedure)."
Hence the following code will run in R:
mySQLDriver <- dbDriver("MySQL")
connect <- dbConnect(mySQLDriver, group = connection)
query <-
"
CREATE PROCEDURE Test.Tester()
BEGIN
/***DO DATA AGGREGATION***/
END
"
sendQuery <- dbSendQuery(connect, query)
dbClearResult(dbListResults(connect)[[1]])
dbDisconnect(connect)

RODBC sqlQuery() returning error messages on successful execution

This is a bit of a time-sensitive/emergency problem. I have some R code that involves a number of SQL queries using the RODBC package. This code runs every morning on a dedicated Linux server - it pulls down some data, does some statistics, and inserts the data back into an MSSQL DB.
Today, our sysarch upgraded to Ubuntu 12.04.1, and since then, I'm having a bunch of problems with ODBC. After making a few changes, we got it so that ODBC connections can be established, but now there's an even bigger problem. Basically, I'm getting error messages from RODBC every time I do a CREATE or DROP command, even though the identified tables are created/dropped. Example (bts.connect is the result of odbcConnect([connection information])):
> sqlQuery(bts.connect, "SELECT OBJECT_ID('tempdb.dbo.#tempscores')")
1 1050250967
So the #tempscores table exists
> sqlQuery(bts.connect, "DROP TABLE #tempscores")
[1] "[RODBC] ERROR: Could not SQLExecDirect 'DROP TABLE #tempscores'"
Even though it exists, we “can’t” drop it
> sqlQuery(bts.connect, "SELECT OBJECT_ID('tempdb.dbo.#tempscores')")
1 NA
But we have dropped it.
> sqlQuery(bts.connect, "CREATE TABLE #tempscores (dummy int)")
[1] "[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE #tempscores (dummy int)'"
We also can’t create it, but it exists and we can SELECT from it:
sqlQuery(bts.connect, "SELECT OBJECT_ID('tempdb.dbo.#tempscores')")
1 1066251024
sqlQuery(bts.connect, "SELECT * FROM #tempscores")
[1] dummy
<0 rows> (or 0-length row.names)
I'm at wits' end, and I really need this to run successfully tomorrow morning for a client. Anybody have any idea what could be causing this strange behavior?

SQL query error with ODBC connection in R using Informix driver

With functionality from the RODBC package, I have successfully created an ODBC but receive error messages when I try to query the database. I am using the INFORMIX 3.31 32 bit driver (version 3.31.00.10287).
channel <- odbcConnect("exampleDSN")
unclass(channel)
[1] 3
attr(,"connection.string")
[1] "DSN=exampleDSN;UID=user;PWD=****;DB=exampleDB;HOST=exampleHOST;SRVR=exampleSRVR;SERV=exampleSERV;PRO=onsoctcp ... (more parameters)"
attr(,"handle_ptr")
<pointer: 0x0264c098>
attr(,"case")
[1] "nochange"
attr(,"id")
[1] 4182
attr(,"believeNRows")
[1] TRUE
attr(,"colQuote")
[1] "\""
attr(,"tabQuote")
[1] "\""
attr(,"interpretDot")
[1] TRUE
attr(,"encoding")
[1] ""
attr(,"rows_at_time")
[1] 100
attr(,"isMySQL")
[1] FALSE
attr(,"call")
odbcDriverConnect(connection = "DSN=exampleDSN")
When I try to query and investigate the structure of the returned object, I receive an error message 'chr [1:2] "42000 -201 [Informix][Informix ODBC Driver][Informix]A syntax error has occurred." ...'
Specifically, I wrote an expression to loop through all tables in the database, retrieve 10 rows, and investigate the structure of the returned object.
for (i in 1:153){res <- sqlFetch(channel, sqlTables(channel, tableType="TABLE")$TABLE_NAME[i], max=10); str(res)}
Each iteration returns the same error message. Any ideas where to start?
ADDITIONAL INFO: When I return the object 'res', I receive the following -
> res
[1] "42000 -201 [Informix][Informix ODBC Driver][Informix]A syntax error has occurred."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'SELECT * FROM \"exampleTABLE\"'"
The error message you quote is:
"[RODBC] ERROR: Could not SQLExecDirect 'SELECT * FROM \"exampleTABLE\"'"
Informix only recognizes table names enclosed in double quotes if the environment DELIMIDENT is set in the environment, either of the server or the client (or both). It doesn't much matter what it is set to; I use DELIMIDENT=1 when I want delimited identifiers.
How did you create the table in the Informix database? Unless you created the table with DELIMIDENT set, the table name will not be case sensitive; you do not need the quotes around the table name.
The fact that you're getting error -201 means you've got through the connection process; that is a good start, and simplifies what follows.
I'm not sure whether you're on a Unix machine or a Windows machine - it often helps to indicate that. On Windows, you might have to set the environment with SETNET32 (an Informix program), or there may be a way to specify the DELIMIDENT in the connect string. On Unix, you probably set it in your environment and the R software picks it up. However, there might be problems if you launch R via some sort of menu button or option in a GUI environment; the chances are that the profile is not executed before the R program is.
You can try using the sqlQuery() function in RODBC to retrieve your results. This is the function I use at work and have never had a problem with it:
sqlQuery(channel, "select top 10 * from exampleTABLE")
You should be able to put all of your queries into a list and iterate through them as you were before:
dat <- lapply(queries, function(x) sqlQuery(channel, x))
where queries is your list of queries and channel is your open ODBC connection. I guess I should also encourage you to close said connection when your done with odbcCloseAll()

Resources