OPENQUERY on SQL Server 2005 64bit behaves weird - sql-server-2005

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'')')

Related

Understanding of difference between MariaDB and SQL Server for a query

I am using MS SQL Server Version 2008 and MariaDB 10.5.
My table and data :
Following is the query i am running on both the databases.
select distinct distributor from market order by city
Found this query is failing in MS SQL Server database with error
SQL Error [145] [S0001]: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Understood the reason why it failing
Reason for - ORDER BY items must appear in the select list if SELECT DISTINCT is specified
when i ran the same query run with MariaDB it ran successful without any error and gave me below output.
So i have doubt why MariaDB is behave different here? ideally it should fail right?
Thanks in advance.
Please try this query
Select distinct col from (
Select col from table order by col
)

Microsoft SQL Server and Oracle SQL Developer

I linked an Oracle Database to my SQL Server in Microsoft SQL Server Management Studio 18. Server Objects -> Linked Servers.
I have a SQL Statement that when I run on the Oracle Developer Tool/Platform it returns the information as expected. But when I run the exact same query on the SQL Server it returns the incorrect results (The actual values in the rows and columns do not match).
What I know.
The table I am query in lives in the Oracle Database.
I can get the same/matching results on the Oracle Developer and SQL Server if I exclude in my WHERE statement anything involving a DATE.
Any thoughts?
The example of the query below.
Works on Oracle Developer but not on MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE('01/03/2020', 'DD/MM/YYYY') AND TO_DATE('10/12/2020','DD/MM/YYYY');
The example of the query below.
Works on both Oracle Developer and MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and BATCHID = 'ThisBAtchID';
You cannot use ORACLE specific functions like TO_DATE in SQL Server calls. You have to execute them remotely using OPENQUERY. OPENQUERY in MSDN
SELECT * FROM OPENQUERY (OracleSvr, 'SELECT * FROM TABLE1
WHERE status = ''Deviation'' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE(''01/03/2020'', ''DD/MM/YYYY'') AND TO_DATE(''10/12/2020'',''DD/MM/YYYY'');');

sql server shortcut for select statement writing

Many times a day I have to write similar queries to get single record:
select t.*
from some_table t
where t.Id = 123456
maybe there is some shortcuts for retrieving single record? Like entering id, table and SQL server generates rest code automatically
In Sql Server Go to
Tools-> Options-> Environments->Keyboard
You will get shortcuts, there you can define your own as well as get the standards.
you can set a short cut for a fully executable query like
select * from table where id =20
but not like below
select * from

Oracle Query in Excel works, but not in 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.

how to update table from linked server in sql server?

I have few tables I need to load from linked firebird server into SQL Server from time to time. I use statement like this:
SELECT * into dekr FROM OPENQUERY ( [PLINK] ,'select * from dekr' )
It takes a while since it's going over network etc, is there a way to update once created
table dekr only with changes since last time?
thanks