Handling Dates from Oracle to SQL Server 2005 using OPENQUERY - sql

I have linked SQL Server 2005 to an Oracle Server, and am using OpenQuery() to pass data from Oracle to SQL Server.
So, for instance, the following query works fine:
SELECT *
FROM OPENQUERY(ORACLE_LINK, 'SELECT FIELD1
FROM ORACLE.TABLE1');
Everything works, unless I ask for a field which is a date/time in the Oracle database. For instance, say that TABLE1 has ten fields, one of which is a timestamp. If I ask for all nine fields except for the timestamp, it works. If I ask:
SELECT *
FROM OPENQUERY(ORACLE_LINK, 'SELECT *
FROM ORACLE.TABLE1');
I get the following error:
OLE DB provider "MSDAORA" for linked server "ORACLE_LINK" returned message "Oracle error occurred, but error message could not be retrieved from Oracle.".
OLE DB provider "MSDAORA" for linked server "ORACLE_LINK" returned message "Data type is not supported.".
I also get the same error if I ask for only the timestamp field.
What is the best way of handling this error? Thanks.

I do it with this function (partial code):
select #StringOut = '{ts ''''' + convert(varchar(20), #DateIn, 20) + ''''' }';
RETURN #StringOut
and this goes into the query:
and procedure_date_dt = ' + dbo.TimestampString(#date) + '
and event_type = ''''Time Notation''''
and it works when I run it.

Related

Problem with Stored Procedures in PHPmyAdmin

Attempting to perform operations with a random integer in SQL.
The following code works perfectly as intended when run as pure SQL, but triggers a syntax error when attempting to save it into a stored procedure.
SET #sample_count = (SELECT count(*)
FROM cinder_sample);
SELECT #sample_count;
SET #random_ID = (SELECT FLOOR(RAND()*#sample_count));
SELECT #random_ID;
Any ideas as to what could be going wrong?
The exact error triggered is:
"The following query has failed: "CREATE DEFINER=root#localhost PROCEDURE play_random_sp() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER DELIMITER // SET #sample_count = (SELECT count() FROM cinder_sample)// SELECT #sample_count// SET #random_ID = (SELECT FLOOR(RAND()#sample_count))// SELECT #random_ID"
MySQL said: #1064 - 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 '// SET #sample_count = (SELECT count(*) FROM cinder_sample)// SELECT' at line 1"

Invalid SQL statement; error when using IF EXISTS() in SQL statement, OLEDB MS ACCESS

I'm trying to build an SQL statement that will insert or update a row in the database depending on whether it exists in the table already. This format seems to work when I interface to an MSSQL server, but does not work when I use an OLEDB connection to MS Access.
IF EXISTS(select * from Resistors where ID = 2816)
update Resistors set
[Part Number]='1234'
where ID = 2816
else
insert into Resistors (
[Part Number]
)
values(
'1234'
)
;
I have validated these two sql commands using the OLEDB connection to MS Access. They work properly but now with the IF EXISTS() portion of the command.
update Resistors set
[Part Number]='1234'
where ID = 2816
insert into Resistors (
[Part Number]
)
values(
'1234'
)
The IF EXISTS() statement appears to match other documentation I have seen on the internet but I must be doing something wrong.
This is the error I receive when I run the command.
Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
This is my connection string
connection.ConnectionString = "Provider = Microsoft Office 12.0 Access Database Engine OLE DB Provider; Data source = " + _database_path;
Is the exists command not supported by this connection interface? Is there a version that supports the exists command?
Unfortunately, ifexists() is not supported in MS-Access sql.
you can use select count() as a work around, if returns 0 then it is not exist.
...
string cmdstr = "SELECT COUNT(*) FROM Resistors where ID = 2816";
....
int count = (int)command.ExecuteScalar();
....
if (count=0)
....
I hope this may help

How to make dynamic query in anypoint studio?

If id is present in flowVars, i will fetch user from database by id. If not present, I will fetch all users. I tried to use this expression but no success:
select * from user #[flowVars.userId != null ? 'where id = ' + flowVars.userId : '']
error is :
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 ''where id = 1'' at line 1 (com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException).
I think it creates single quote in query.
Insert into info Values (#[payload.Cname], #[payload.orderid], #[payload.customerid], #[payload.allergies])
you can write like this a dynamic query

SAS Pass Through OpenQuery into iHistorian Error

I am doing a pass through query using SAS into iHistorian Interactive SQL. The code is listed below. I know the pass through code works; I ran the quoted code through iHistorian Interactive SQL itself and it returned the required data. I get the following error when I attempt the provided code.
ERROR: CLI describe error: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot initialize the data source object of OLE DB
provider "IhOLEDB.iHistorian" for linked server "fsp1".
Has anyone tried this with iHistorian? Does anyone know what's going on here? Thanks in advance.
proc sql;
connect to odbc as conn(dsn="FSPPDW" uid=FontaneF);
create table work.Stuff as
select * from connection to conn (
select * from OPENQUERY(fsp1,
'SELECT
tagname,
timestamp,
value as Signal
FROM ihrawdata
WHERE timestamp > 10/23/2015 AND timestamp < 10/25/2015
AND (tagname = Fsdfeee:000)
AND intervalmilliseconds = 60000
AND samplingmode=interpolated'
)
);
disconnect from conn;
;
quit;

Postgres CASE statement not working in SQL Server

I've written a query in postgres (on PGAdmin) and I'd like to put it in SSMS2012 for reporting purposes however it doesn't seem to allow the CASE statement, this is my query:
SELECT *
FROM OPENQUERY (POSTGRESQL,
'SELECT
Table1 AS Table_1,
CASE Table2
WHEN 75887 THEN ''1''
WHEN 75888 THEN ''2''
WHEN 75889 THEN ''3''
WHEN 75890 THEN ''4''
WHEN 75891 THEN ''5''
WHEN 75892 THEN ''6''
END AS Table_2,
DateTable1 AS DateTime
FROM SuperTable1 ')
And I'm getting the error:
OLE DB provider "MSDASQL" for linked server "POSTGRESQL" returned
message "Requested conversion is not supported.".
Msg 7341, Level 16, State 2, Line 1
Cannot get the current row value of column "[MSDASQL].table_2" from OLE DB provider "MSDASQL" for linked server "POSTGRESQL".
I've noticed if I remove the whole case statement it has no issues retreiving the data in the columns in the SELECT statement and the whole thing (including the CASE statement) works fine in PGAdmin.
Try simplify the query
Run 4 separated querys to determinate which one is giving you the problem.
Also try using 1 instead of ''1''
SELECT Table1 AS Table_1
SELECT DateTable1 AS DateTime
SELECT
CASE Table2
WHEN 75887 THEN 1
END AS Table_2
SELECT
CASE Table2
WHEN 75887 THEN ''1''
END AS Table_2