SAS Pass Through OpenQuery into iHistorian Error - sql

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;

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

Must declare the scalar variable SQL with Matlab

I have a SQL script that works fine in Microsoft SQL Sever Management Studio. It looks something like this:
DECLARE #CONST_STARTDATE DATETIME
DECLARE #CONST_ENDDATE DATETIME
SET #CONST_STARTDATE = '2017-01-01';
SET #CONST_ENDDATE = '2050-05-05';
WITH abc AS ( SELECT * FROM tablename WHERE StartDate BETWEEN #CONST_STARTDATE AND #CONST_ENDDATE )
However, it does not seem to work when I run this in MATLAB as follows:
conn = database('db','user','pw');
qfile = 'path\to\file.sql'
results = runsqlscript(conn,qfile);
It returns the following errors:
Query Batch 1: No ResultSet was produced
Query Batch 2: [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "#CONST_ENDDATE".
Query Batch 3: [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "#CONST_STARTDATE".
What is the reason for this error in MATLAB, whereas it works fine on the Sever Studio?

Special character in stored procedure

I want to use a SQL procedure to pull the data from SQL to SAS EG.
But a value in one parameter (INVNO)is TN/2015-16/0005-13 ie special character.
This is causing an error:
ERROR: CLI prepare error: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near '/'.
SQL statement: execute SAS_INV_RPT AMRUTFAC, SDAB, 1, TN05/000023/14-15, TN05/000024/14-15.
Can anyone come up with a workaround for this?
PROC SQL;
CONNECT TO ODBC (DSN='AHCLOD' uid=&uid pwd=&pwd);
CREATE TABLE sas_inv_rpt AS
SELECT * FROM connection to ODBC (
execute SAS_INV_RPT
&COMPANY_CODE,
&LOCATION_CODE,
&LANG_ID,
&INVNO_min,
&INVNO_max);
DISCONNECT FROM ODBC;
QUIT;
Also can I use range for text value with INVNO_min and INVNO_max prompts
To answer the first question:
Take a look at: SAS %QUOTE and %NRQUOTE MACRO Functions
PROC SQL;
CONNECT TO ODBC (DSN='AHCLOD' uid=&uid pwd=&pwd);
CREATE TABLE sas_inv_rpt AS
SELECT * FROM connection to ODBC
(execute
SAS_INV_RPT
&COMPANY_CODE,
&LOCATION_CODE,
&LANG_ID,
%QUOTE(&INVNO_min), /* Mask any special chars in the resolved text*/
%QUOTE(&INVNO_max) );
DISCONNECT FROM ODBC;
QUIT;
RUN;

Handling Dates from Oracle to SQL Server 2005 using OPENQUERY

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.