Special character in stored procedure - sql

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;

Related

Passing a value from Netezza back to SAS using a macro variable

we're using SAS 7.13 HF1 (7.100.3.5419) (64-bit)
I'm currently looking at a post that shows how to pass a value from SAS to a database that you're connecting in to. Here is the example below. You can see they take the Macro variable StartDate and pass it into Teradata to be used in the query.
%let StartDate = 2016/01/01;
proc sql;
connect to teradata
(BULKLOAD=YES MODE=TERADATA user=&user. Password=&passwd.);
CREATE TABLE WorkTest.test AS
select * from connection to teradata
(
SELECT
TOP 10 *
FROM SomeCalendarData
WHERE SomeDate = %bquote('&StartDate.');
);
quit;
I want to go the other way.
How can I read a value from a similar query, only my DB is Netezza, and somehow pass it to a macro variable in SAS?
Thanks!
You would use the
SELECT <expression> INTO :<macro_var>'
statement. This is available in the PROC SQL query but not in the pass-through code, so it would look something like
proc sql;
connect to teradata
(BULKLOAD=YES MODE=TERADATA user=&user. Password=&passwd.);
select somedate into :my_macro_var from connection to teradata
(
SELECT somedate
FROM SomeCalendarData
WHERE id = 101;
);
quit;
See the docs here: http://support.sas.com/documentation/cdl/en/sqlproc/63043/HTML/default/viewer.htm#n1tupenuhmu1j0n19d3curl9igt4.htm

Sas process sql

I am trying to select from a SQL server table that both has dashes in the name and is greater than 32 characters.
I have tried pass through and quotes but no joy.
It's very unlikely that I could get a view produced and only have read access.
proc sql;
drop table poss_gw1;
create table poss_gw1 as ( select * from cdb.'''form_Garden_waste_service_AF-Form-59fb9946-0f6e-4cd9-‌​9b30-82fc5d96ec71'''‌​n as agg);
quit;
proc sql;
connect to odbc(dsn=FirmstepReporting user=myname pwd=mypwd);
Create table work.tmp_gw as select * from connection to odbc (select * from "'form_Garden_waste_service_AF-Form-59fb9946-0f6e-4cd9-9b30-‌​82fc5d96ec71'"n);
disconnect from odbc;
quit;
Any one have any ideas?
You need to use SQL Server syntax in the pass thru code.
create table work.tmp_gw as
select * from connection to odbc
(select *
from "form_Garden_waste_service_AF-Form-59fb9946-0f6e-4cd9-9b30-‌​82fc5d96ec71"
);
If your variable names are also not valid for SAS then you will need to change the name in the pass thru code also.
create table work.tmp_gw as
select * from connection to odbc
(select id
, "invalid-name" as valid_name
from "form_Garden_waste_service_AF-Form-59fb9946-0f6e-4cd9-9b30-‌​82fc5d96ec71"
);

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;

Use macro variables in Proc SQL in SAS

Two macro variables are defined for table schema and table name and they are used in the proc sql statement in SAS (postgresql interface to Amazon RedShift), sql statement can't be read correctly.
%let table_schema = 'sales';
%let table_name = 'summary_table';
proc sql;
connect to ODBC(DSN='redshift_db', user=masteruser password='*pwd');
create table column_names as
select * from connection to odbc(
select distinct(column_name) as col_name from information_schema.columns where table_schema = &table_schema and table_name = &table_name;
);
create table dt as
select * from connection to odbc(
select * from &table_schema..&table_name;
);
QUIT;
The first table creation throws an error:
ERROR: CLI describe error: ERROR: column "summary_table" does not exist in columns;
The "summary_table" actually exists.
The second table creation throws an error:
ERROR: CLI describe error: ERROR: syntax error at or near "'sales'"; No query has been executed with that handle
which is invalid either.
Check your syntax.
Look at the ERROR: column "summary_table" does not exist in columns;
What that is saying is that the COLUMN named "summary_table" does not exist on the table named "columns". The where clause is trying to compare 2 columns, not a column and a string.
Write the query without macros and get that to work. Then attempt to put the macros in.
This is a syntax issue with the PostGreSQL ODBC driver, not SAS.
I haven't used the PostGreSQL driver before but I know different DBs handle quotes differently. It may be that PostGreSQL wants double quotes instead of single. If this is the case then I would change your code like so:
%let table_schema = sales;
%let table_name = summary_table;
proc sql;
connect to ODBC(DSN='redshift_db', user=masteruser password='*pwd');
create table column_names as
select * from connection to odbc(
select distinct(column_name) as col_name from information_schema.columns where table_schema = "&table_schema" and table_name = "&table_name";
);
create table dt as
select * from connection to odbc(
select * from &table_schema..&table_name;
);
QUIT;
Note that I've removed the single quotes from the %let statments and added double quotes to the where clause in the first query.
Removing the singles quotes from the macro vars will also fix the syntax error you are experiencing with the second query as you were effectively trying to do the following:
select * from 'sales'.'summary_table';
... which is most likely not correct. Again I haven't used PostGreSQL so I can't say for certain - please correct me if I'm wrong and that is correct syntax for it.
Finally, I'd recommend that when working with macro variables in general, that you don't put single quotes around your strings. ie.
%let table_schema = 'sales'; * AVOID THIS WHEN POSSIBLE;
vs
%let table_schema = sales; * USE THIS APPROACH WHEN POSSIBLE;
Put double quotes around the places where macro vars are used instead and you'll probably find things a little easier. Of course there's always circumstances where you may want quotes in the value of the macro variable, but as a general rule of thumb I avoid doing this whenever possible.

Firebird IBOConsole SQL Drop Table if exist not working

I'm pretty new on Firebird.. trying to write a query that drops the table if exists through IBOConsole.
I have written the following sql statement,
EXECUTE block as
BEGIN
if (exists(
SELECT 1 FROM RDB$RELATIONS Where RDB$RELATION_NAME = 'ZZGTTUNIQUEID'))
then
execute statement 'DROP TABLE ZZGTTUNIQUEID';
END
but getting the following result..
ISC ERROR CODE:335544569
ISC ERROR MESSAGE:
Dynamic SQL Error
SQL error code = -104
Unexpected end of command - line 6, column 19
i'm not sure what might be wrong?
In IBOConsole I also experienced problems using the EXECUTE BLOCK statement, resulting in a 'Problem in BindingCursor' message and not executing the statement on the database. Use IBExpert's script executive or FlameRobin instead and it will work.