Oracle Query in Excel works, but not in SQL - sql

I'm currently trying to query an Oracle server and pull in data into a SQL server.
Now what I've got is this query:
select * from openquery(databasename, 'SELECT * FROM RICALM.REQUEST_TIMESTAMP_EXT DueDate
WHERE DueDate.NAME = "com.ibm.team.apt.attribute.constraintdate"')
If I toss just the query:
SELECT * FROM RICALM.REQUEST_TIMESTAMP_EXT DueDate
WHERE DueDate.NAME = 'com.ibm.team.apt.attribute.constraintdate'
into excel, where I've created an oracle link, I can pull the data just fine.
However, when I pull the data in SQL, I can pull the whole table if I just do "SELECT * FROM RICALM.REQUEST_TIMESTAMP.EXT" but when I add in the WHERE clause I get an error message:
OLE DB provider "OraOLEDB.Oracle" for linked server "databasename" returned message "ORA-00972: identifier is too long".
Any workaround for this? I'd like to just schedule a query like this to run each night so I don't have to deal with refreshing an excel table.
Thanks!

Try this for the openquery you are using:
select * from openquery(databasename, 'SELECT * FROM RICALM.REQUEST_TIMESTAMP_EXT DueDate WHERE DueDate.NAME = ''com.ibm.team.apt.attribute.constraintdate''')
Difference #1 being replacing the double quotes:
"
with two single quotes, which should resolve to a single quote:
''
Difference #2 being putting it as one line.

Related

import data from table named "document" in odbc database with powerbi

I'm connected to a database that contains a table named "document", this term is also an sql term at the same time so when I query it, it doesn't recognize it.
I'm used to just add some quotation marks to query it in dbeaver and it works well.
select * from "document"`
But when I want to write my query in PowerBi to import this table, it doesn't work with the quotation marks.
I also tried it in this format:
select * from public.document
Or I tried to don't write any sql query and just select it when I connect to the odbc database, but at the moment of update, the process starts and doesn't stop.
If the table really has double quotes in its name, one of these should work:
Using backtick
select * from `"document"`
Using double quote as escape:
select * from ""document""

dbGetQuery() Error: Can't retrieve the table or table data using SQL in R

I am trying to use the SQL query to count the number of rows of myid in Table_1 data frame. The data frame is already present in R Environment and I retrieved it by following query.
Table_1 <- dbGetQuery(conn, "Select * FROM Patients") .
Now to count() the number of rows of myid in Table_1, I use following query and it says
count_myid <- dbGetQuery(conn, "Select count(myid) FROM Table_1")
Invalid object name 'Table_1'[Microsoft][odbc sql server driver]
[sql server] Statement(s) could not be prepared
Whereas, I checked the above mentioned query for another Table and it works perfectly fine for another table. I spent hours on this to figure out the problem but I couldn't that what is actually wrong in this query and why its not working. I know it can be done using different functions of R. I did used dbReadTable() and all the same queries mentioned in this Question, but not working.
If I try to retrieve it directly from SQL database and not from the R Environment, then it works fine. From SQL DB, I use the following query.
count_myid <- dbGetQuery(conn, "Select count(myid) FROM MyProj.dbo.Patients")

How can you convert/cast text datatypes from a PostgreSQL database to a linked MS SQL Server?

Good morning/afternoon! Been working on this problem most of the day so I figured it was time to appeal to a larger audience.
I'm running Microsoft SQL Server 2012. I have created a "Linked Server" to a PostgreSQL server. When I try to issue a query to the PostgreSQL server I get this:
SELECT *
FROM OPENQUERY(MYDB, 'SELECT notes from remote_view LIMIT 50');
Msg 7347, Level 16, State 1, Line 1
OLE DB provider 'MSDASQL' for linked server 'MYDB' returned data that does not match expected data length for column '[MSDASQL].notes'. The (maximum) expected data length is 8000, while the returned data length is 9088.
If I truncate the field (using LEFT(notes, 4000)) I can get it to work. The field on the PostgreSQL table is the "text" data type.
Any ideas how to get the data to come across without losing any of it?
UPDATE #1:
Trying to cast the value to varchar(max) yields this:
SELECT *
FROM OPENQUERY(MYDB, 'SELECT cast(notes as varchar(max)) as notes2 from remote_view LIMIT 50');
OLE DB provider "MSDASQL" for linked server "QPID" returned message "ERROR: syntax error at or near "max";
No query has been executed with that handle".
Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "MSDASQL" for linked server "QPID".
If I try to cast it as varchar(8000), it gives me this:
OLE DB provider "MSDASQL" for linked server "QPID" returned message "Requested conversion is not supported.".
I had the exact problem. I found a workaround, if length of your text data is not bigger than Postgresql's character varying size limit (10485760). You just need to change Postgresql ODBC MaxLongVarchar setting to 10485760 like the following screenshot as a first step.
After that you have three options:
You can change your text data field to character varying (10485760) at PostgreSql Server, if it's possible. The following syntax will work without any problems.
SELECT * FROM LINKEDSERVER.dbname.schemaname.tablename
If you can't change original table, you can create a view on postgresql which transforms all text fields as originaltextfield::varchar(10485760) and select from postgresql view, instead of table
SELECT * FROM LINKEDSERVER.dbname.schemaname.viewname
You can use OPENQUERY as follows
SELECT * FROM OPENQUERY(LINKEDSERVER, 'SELECT id, textfield::varchar(10485760) FROM schemaname.tablename')
Here's what I came up with that seemed to work for this issue. Break the main text field into several smaller VARCHAR fields, inside the OPENQUERY. To do this, CAST the field as a very large VARCHAR value. Then do LEFT/SUBSTRING of that value, to grab a small chunk of it. Outside of the OPENQUERY CAST each of the fields you created as VARCHAR(MAX) and concatenate them together. Using the original example, it would be something like this..
SELECT CAST(Notes1 AS VARCHAR(MAX))
+ CAST(Notes2 AS VARCHAR(MAX))
...
+ CAST(Notes5 AS VARCHAR(MAX)) AS Notes
FROM OPENQUERY(MYDB, 'SELECT
LEFT(CAST(Notes AS VARCHAR(20000),3500) AS Notes1
,SUBSTRING(CAST(Notes AS VARCHAR(20000),3501,3500) AS Notes2
...
,SUBSTRING(CAST(Notes AS VARCHAR(20000),14004,3500) AS Notes5
FROM remote_view')
You are almost done:
SELECT *
FROM OPENQUERY(MYDB, 'SELECT notes::varchar(10000) from remote_view LIMIT 50');
returned data length is 9088, so your buffer should be big enough to fit!
I had the same problem with my linked server and Openquery. I like to add my 2 cents here as solution is little different and easy compared to above solutions. The reason for taking fixed length for string is because that's how it is set in your ODBC driver. Go to your ODBC driver, Advance options and update the text box that says string length to 8000 or more to solve the problem.
Hope this helps.
Thanks!
My issue was an apostrophe in the company name. i am using openquery through a linked server to postgres 9.1
i got a data length mismatch. once i cast it to varchar 1000 it came through.
select * from OpenQuery(CMS, 'select dealer,
company::varchar(1000)
,agent,carrier,add1,city,state,zip,phone,taxid from m1') as GCP)
VARCHAR(max) can store only 8000, try the TEXT datatype instead

Error trying to Select x rows from DB2 (V4R5M0) via sql server linked server using OPENQUERY

I have a Linked Server from SQL Server 2008 R2, to a DB2 Database (V4R5M0) using OLE DB provider "IBMDA400"
Linked Server Detials
EXEC master.dbo.sp_addlinkedserver
#server = N'JTEST', #srvproduct=N'IBM OLE DB Provider for DB2',
#provider=N'IBMDA400', #datasrc=N'TestName'
This works fine:
SELECT * FROM OPENQUERY(JTEST, 'Select * from QSYS2.SYSCOLUMNS')
But the following statement produces an error:
SELECT * FROM OPENQUERY(JTEST, 'Select * from QSYS2.SYSCOLUMNS FETCH FIRST 10 ROWS ONLY')
Error
LE DB provider "IBMDA400" for linked server "JTEST" returned message
"SQL0199: Keyword FETCH not expected. Valid tokens: FOR WITH ORDER
UNION OPTIMIZE. Cause . . . . . : The keyword FETCH was not expected
here. A syntax error was detected at keyword FETCH. The partial list
of valid tokens is FOR WITH ORDER UNION OPTIMIZE. This list assumes
that the statement is correct up to the unexpected keyword. The error
may be earlier in the statement but the syntax of the statement seems
to be valid up to this point. Recovery . . . : Examine the SQL
statement in the area of the specified keyword. A colon or SQL
delimiter may be missing. SQL requires reserved words to be delimited
when they are used as a name. Correct the SQL statement and try the
request again.". Msg 7321, Level 16, State 2, Line 1 An error occurred
while preparing the query "Select * from QSYS2.SYSCOLUMNS FETCH FIRST
10 ROWS ONLY" for execution against OLE DB provider "IBMDA400" for
linked server "JTEST".
I think it's because FETCH FIRST X ROWS ONLY is not supported in this version of DB2? But is there any way of selecting only a limited record set in this version?
Wouldn't FETCH FIRST need an ORDER BY? Otherwise, which 10 rows would it get?
As far as I can see in the DB2 SQL docs, it is supported on current and older versions, although it doesn't state (and it not clear) if ORDER BY is mandatory with FETCH FIRST
Fetch first clause work only from V5R1 OS400 version.
V4R5M0 is too old
There's a workaround:
select * from (
SELECT syscolumns.* , row_number() over() as nre FROM syscolumns
ORDER BY COLUMN_NAME ) as columns
where nre<10
You can try it
DEpe

OPENQUERY on SQL Server 2005 64bit behaves weird

I just recently moved a SQL DB from SQL2005 32 bit --> 64 bit. I am having an issue connecting to Oracle using the OraOLEDB.Oracle Provider.
I was able to install Oracle 10G Client , ODAC 64 bit. I was also able to add a linked server to the Oracle instance. I am able to run a query using the linked server name directly:
SELECT top 10 *
FROM [DB0PBB0]..[DB0PBB0].[DM_CLICK]
So far it is good, however, the problem occurs when I try to use OPENQUERY. I tried the following:
select * from
OPENQUERY(DB0PBB0,'select * from DB0PBB0.DM_CLICK where Date_stamp <''24-Jul-09'' and Date_stamp >= ''23-Jul-09'' ')
SET FMTONLY OFF
select * from
OPENQUERY(DB0PBB0,'select * from DB0PBB0.DM_CLICK where Date_stamp <''24-Jul-09'' and Date_stamp >= ''23-Jul-09'' ')
and I get only the column names, no rows :(
if I run this script:
SET FMTONLY OFF
select * from
OPENQUERY(DB0PBB0,'select ''hello'' from dual ')
I get
hello
My question is, has anyone tried running OPENQUERY against Oracle from a SQL05 64bit ? Any ?Idea why would I get only columns back instead of data? I tried the same query on another server with the same link and it worked, it returned rows.
since you are getting rows with the SELECT * FROM DUAL chances are that it is a query issue. Regarding your query one potential pitfall is that you are comparing what looks like a date column (DATE_STAMP) with a VARCHAR.
You should not rely on implicit conversions to compare dates. Instead you should use the appropriate explicit functions, for exemple:
select * from
OPENQUERY(DB0PBB0,'select *
from DB0PBB0.DM_CLICK
where Date_stamp < to_date(''24-Jul-09'', ''dd-mon-rr'')
and Date_stamp >= to_date(''23-Jul-09'', ''dd-mon-rr'')')