I am using the R DBI package to query a oracle database.
When using inner joins it gives me the following error.
Plain SELECTS without INNER JOIN works fine. Why?
Error: nanodbc/nanodbc.cpp:1617: 00000: [Oracle][ODBC][Ora]ORA-00933: SQL command not properly ended
DBI::dbGetQuery(con_ora, "SELECT dat.LP_ZP_ID, dat.LP_TIMESTAMP_LOC, dat.LP_WERT, zp.ZP_ZAEHLPUNKT
FROM EDM_LP_DATEN dat
INNER_JOIN EDM_ZAEHLPUNKT zp ON zp.ZP_ID=dat.LP_ZP_ID
WHERE dat.ZP_ID = 111 AND dat.LP_TIMESTAMP_LOC >= TIMESTAMP '2021-01-01 00:00:00'")
What's obviously wrong is this:
INNER_JOIN EDM_ZAEHLPUNKT
It is not inner_join but inner join (without underscore).
The rest of your query looks OK.
Related
I have a query that works perfectly in SSMS. But when running the query in R using the DBI package, I receive several multipart identifier errors: The multi-part identifier: "rt.secondary_id" could not be bound, "rt.third_id" could not be bound, and "t2.important" could not be bound.
select t1.[main_id]
,rt.secondary_id
,rt.third_id
,t1.[date_col]
,t2.important
from t1
inner join rt on t1.main_id = rt.main_id
inner join t2 on rt.main_id = t2.main_id
inner join (select t1.main_id, max(t1.date_col) as upload_time from t1 group by t1.main_id) AS ag ON t1.main_id = ag.main_id AND t1.date_col = ag.upload_time
The unique identifier in t1 is the combination of main_id and date_col, and this query finds the most recent entry in t1 for a given main_id.
Not exactly sure if my query is structured in a poor way or this is an R issue. I've tried adding SET NOCOUNT ON to the query based on what I thought might be related issues elsewhere on stackoverflow, but no dice.
I found out what my issue was- silly (but time consuming) mistake on my part... but essentially, I was bringing my SQL query into R via paste(scan(...), collapse = " "). I had a comment in my SQL query, --, which could not be read correctly by R. Deleting the comment OR switching the comment to /* ... */ syntax fixes the problem.
I tested the code before and it didn't give me any errors.
Now there is an error "sqldf: table T1 already in test".
How can I fix it?
ret <- sqldf("SELECT T1.*, T2.score FROM T1 JOIN T2 USING(date)")
You just need to execute:
sqldf("drop table test.T1")
You may have a dataframe with the same name as your database table. Clear your environment and try again.
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.
I recently found the libname statement is very handy to access tables reside in odbc. But I don't know how to access those tables having a two-level names. Below is an example.
Old code I use (this code works):
proc sql;
connect to odbc(dsn=edw_nz user=&username. pw=&password.);
create table test select * from connection to odbc(
select *
from EDW_XYZ_PROD01..Table_xyz);
quit;
Now, I want to use the libname facility to access the odbc:
libname edw odbc database=edw_nz user=&username. pw=&password.;
proc sql;
create table test as
select *
from edw.EDW_XYZ_PROD01..Table_xyz;
quit;
I got this error:
ERROR 22-322: Syntax error, expecting one of the following: a name, (, ), ',', ANSIMISS, AS, CROSS, EXCEPT, FULL, GROUP, HAVING, INNER, INTERSECT, JOIN, LEFT, NATURAL, NOMISS, OUTER, RIGHT, UNION, WHERE.
ERROR 200-322: The symbol is not recognized and will be ignored.
Any one can help?
Thanks a lot!
SAS cannot handle 3 level names.
You need to specify the schema/database inside the libname section. You have a few options (read the doc for all the options).
We use ODBC to connect to our SQL server instances like this:
libname pdata odbc complete='DSN=SQLServerProd;Database=MyDatabase';
The complete= option allows you specify the full ODBC connection string. This should allow you to specify the database/schema.
Is EDW_XYZ_PROD01 a schema or something?
I think you might have to specify that on the datasource= option. For example:
libname mydblib odbc user=testuser password=testpass datasrc=mydatasource;
Have you tried the schema= option on the libname statement. The schema is equivalent to the first level name.
libname edw odbc database=edw_nz user=&username. pw=&password. schema=edw;
proc sql;
create table test as
select *
from EDW_XYZ_PROD01..Table_xyz;
quit;
I have a Teradata query that runs correctly in the Teradata SQL Assistant, but shows an error when written in a sqoop script.
The query does a join on three tables using a condition like this:
...
From tableA as A \
Inner Join tableB as B\
Inner Join tableC as C \
On (B.ID = C.ID and (100*year(A.dt) + month(A.dt)) = C.curr_dt) \
...
The error that I get in sqoop comes from the multiplication.
15/01/06 00:44:01 ERROR manager.SqlManager: Error executing statement: com.teradata.jdbc.jdbc_4.util.JDBCException: [Teradata Database] [TeraJDBC xx.yy.zz.ww] [Error 3706] [SQLState 42000] Syntax error: expected something between '*' and the 'year' keyword.
Is it not possible to do arithmetic operations during a join in sqoop? I haven't found any resources to help me out with this. Any help would be appreciated.
Thanks!
YEAR and MONTH are not valid Teradata SQL, both are ODBC syntax, which is automatically rewritten by the ODBC driver.
Try EXTRACT(YEAR FROM A.dt) instead.