SAS - Insert statement into existing SQL table - sql

Here is my code:
PROC SQL;
connect to odbc (dsn=ODC uid=sa pwd=XXXXX);
EXECUTE ( INSERT INTO dbo.tblDLA_Backup SELECT * FROM &dlafile.) BY ODBC;
disconnect from odbc;
quit;
Im getting this error
ERROR: CLI execute error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'work.dlabackup'.
If i do this:
proc sql;
connect to odbc (dsn=ODC uid=sa pwd=XXXXX);
insert into tblDLA_Backup SELECT * FROM WORK.DLABACKUP;
disconnect from odbc;
quit;
I get this error:
ERROR: File WORK.TBLDLA_BACKUP.DATA does not exist.
Why is it that I can't reference my SAS dataset and just insert? it should be simple as that..

The first error occurs because you are executing an SQL instruction on SQL Server, and not locally. And since this instruction contains a reference to your local SAS dataset, an error occurs because SQL server thinks it is a table in its own database.
You take a wrong approach on that one.
Your second approach is correct because you are executing the SQL in SAS, which both knows the local dataset and the SQL server tables. And it is syntactically valid at first glance.
The error is clear: SAS doesn't found the local dataset WORK.TBLDLA_BACKUP
Thus, verify if this dataset exists and is not corrupted:
in your explorer window, click on Libraries, then Work, and verify if TBLDLA_BACKUP is there, and if yes open it and check if you see your data.
I can't say more at this point, but you should hopefully discover something.

You need to use libref it you want to write into the foreign database.
libname sqldb odbc dsn=ODC uid=sa pwd=XXXXX ;
PROC SQL;
INSERT INTO SQLDB.tblDLA_Backup SELECT * FROM &dlafile.;
quit;
Note that you also need fix it so that your macro variable contains the name of a table that exists.

Related

How to pull data through BigQuery stored procedure in SAS

I'm trying to pull data through BigQuery stored procedure in SAS. Here is my code:
proc sql;
&bq_connection;
create table test as
select * from connection to &bq_conn_name
(
CALL `project.schema.stp_name`();
);
quit;
The stored procedured works fine in GCP console, but I'm error the below error in SAS:
ERROR: CLI prepare error: Message text truncated.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
Any help would be appreciated!!
Thanks.

'Invalid column name' error when using Proc SQL in SAS (WPS)

I am testing a very small macro which uses a Proc SQL command to extract from a SQL database. I have used this many times and understand how it works but this time it is stumping me!
%macro Correlation(dsn,db,prevdb,prodcode,sqltable,var,brick);
proc sql;
connect to ODBC (required="DSN=&dsn;" );
create table comp_correlation as select * from connection to ODBC
(select a.Table_Name,
a.Variable,
a.Correlation as Current_Corr
from DBTest.dbo.MetaData as a
where Product_Code=&prodcode. and Table_Name=&sqltable. and
variable=&var.);
disconnect from ODBC;
quit;
%mend;
I am then calling this macro with the following parameters. My server name is censored but I can assure you it connects successfully.
%Correlation(********, A2019, A2018, HouseValues, Houses, Value);
However, it returns the following error:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'HouseValues'.
Anyone know why this is happening? I am not even trying to select the Product Code as a column, it is only in my where statement?
it seems you have missed one parameter in the below call :
%Correlation(********, A2019, A2018, HouseValues, Houses, Value);
Is it a typo or i interpreted it wrong ?
thanks
I have now fixed this using the %bquote macro.
%macro Correlation(dsn,db,prevdb,prodcode,sqltable,var,brick);
proc sql;
connect to ODBC (required="DSN=&dsn;" );
create table comp_correlation as select * from connection to ODBC
(select a.Table_Name,
a.Variable,
a.Correlation as Current_Corr
from DBTest.dbo.MetaData as a
where Product_Code=%bquote('&prodcode') and Table_Name=%bquote('&sqltable') and
variable=%bquote('&var'));
disconnect from ODBC;
quit;
%mend;
Sounds like your actual error was that you passed a string value to your remote database without quotes. You could solve that by passing the quotes in the macro call.
%macro Correlation(dsn,db,prevdb,prodcode,sqltable,var,brick);
proc sql;
connect to ODBC (required="DSN=&dsn;" );
create table comp_correlation as select * from connection to ODBC
(select a.Table_Name,
a.Variable,
a.Correlation as Current_Corr
from DBTest.dbo.MetaData as a
where Product_Code=&prodcode. and Table_Name=&sqltable. and
variable=&var.);
disconnect from ODBC;
quit;
%mend;
%Correlation(dsn=********,db=A2019,prevdb=A2018
,prodcode='HouseValues', sqltable='Houses', var='Value');
Note that you can use parameter names in the macro call, even for parameters that are defined to allow them to also be called by position.
You could also make your macro a little smarter and have it add the quotes. You could even make it smart enough to remove any optional existing double quotes around the value and replace them with single quotes.
%let prodcode=%sysfunc(quote(%qsysfunc(dequote(%superq(prodcode)))),%str(%'));
Then you could call it anyway you want.
prodcode=HouseValues
prodcode="HouseValues"
prodcode='HouseValues'

How to Truncate Multiple Table in SSIS ( using ADO.NET Destination and Oracle Database)

I got a little trouble in SSIS. I have multiple table and i want adding Truncate statement so that table can't create double data.
This is the image of package that I made :
each Data Flow, i used Flat File Source and ADO NET Destination.
And then, in Execute SQL Task i want to apply Truncate Table
After that, i have error message :
"[Execute SQL Task] Error: Executing the query "truncate table Table1
truncate table Tabl..." failed with the following error: "ERROR
[HY000] [Oracle][ODBC][Ora]ORA-00911: invalid character". Possible
failure reasons: Problems with the query, "ResultSet" property not set
correctly, parameters not set correctly, or connection not established
correctly."
What i must suppose to do?
P.S
Sorry if my english is not good
Since the destination is an Oracle Database you should use this syntax:
begin
execute immediate 'truncate table t1';
execute immediate 'truncate table t2';
end;
Does this syntax work:
truncate table table1;
truncate table table2;
Note the semi colons.

How to connect correctly to the database?

I am not sure why I would have an error when trying to connect to odbc
proc sql;
24 create table xxx.test as
25 (select * from nxxxe.nxrxxt);
1) the connection is successful
2) libref successful
I did not execute any queries. How come I have a syntax error? I even did not have any )
You do have parentheses in your query; try removing them:
proc sql;
create table kyle.test as
select *
from noagree.no_agreement_list;
quit;
But try this with a small table to begin with. Also, especially with Teradata, it's very important that you specify the index. Using the code you have, the first SAS variable in the data set will be used as the index. A poorly chosen index can have seriously negative consequences on your database (affecting other users as well as yourself.
I use SAS/Access to Teradata myself so I don't know if these options are available with ODBC, but here is an example of how to explicitly define the index and various column types:
data kyle.test
( dbcreate_table_opts='primary index(INDEX_COLUMN)'
dbtype=( INDEX_COLUMN='INTEGER NOT NULL'
, USER_NAME='VARCHAR(120) CHARACTER SET LATIN NOT CASESPECIFIC'
, PHONE_NUMBER='CHAR(10)'
, CONTACT_DATE="DATE FORMAT 'yyyy-mm-dd'"
, FROM_LINE_NUM='SMALLINT'
, DOLLAR_DATA='DECIMAL(15,2)'
)
);
set noagree.no_agreement_list;
run;
Note this uses a normal SAS data step rather than PROC SQL; any variable in the data set that is not listed in the dbtype option will be copied using the standard SAS conversion.
Finally, once you get this to work, be sure to run a 'collect statistics' step, which you can do using the SQL Assistant tool (SQLA). I assume you have access to SQLA, especially if you have write permission to some database.

Syntax error on stored procedure call to linked server?

I've got an Informix machine I've configured as a linked server in SQL Server.
I can remotely send, and receive the results of, a query for, say
SELECT * FROM linkedServer.instanceName.database.myTable
but can't run the
linkedServer.instanceName.database.selectAllFromMYTABLE
stored procedure.
The error message I'm getting returned is "[Informix][Informix ODBC Driver][Informix]A syntax error has occurred." which is not massively helpful, except that it tells me that my request was received in some form...
Could someone tell me what the correct calling syntax would be to execute an Informix stored procedure via SQL Server? Presumably I'm screwing up the stored procedure call, because the stored procedure can be verified to be working fine on the Informix server.
EDIT: Adding the full text of a stored procedure I am testing, in order to verify I'm not doing something stupid in Informix which is causing a knock-on problem with the SQL Server execution:
CREATE FUNCTION sp_testSP()
RETURNING char(20) as item_no
DEFINE item_no char(20);
FOREACH
SELECT table_name.item_code
INTO item_no
FROM table_name
WHERE table_name.item_code LIKE 'test%'
RETURN item_no WITH RESUME;
END FOREACH;
END FUNCTION
As I've mentioned, this appears to work fine in RazorSQL, which I have connected to Informix, but maybe seeing this will jog someone's memory with some reason why SQL Server can't work with this return method...
Have you tried using OpenQuery?
http://msdn.microsoft.com/en-us/library/ms188427.aspx
In native Informix, you would write (approximately):
EXECUTE PROCEDURE database#linkedServer:selectAllFromMYTABLE();
I'm not sure quite where you'd fit an instance name into that - the 'linkedServer' corresponds to an instance name (to my way of thinking). The nearest approaches would be:
database#linkedServer:instancename.selectAllFromMyTABLE()
instancename#linkedServer:database.selectAllFromMyTABLE()
However, that is via the native Informix interfaces. If you go via SQL Server, then the syntax probably needs to be the native SQL Server syntax for invoking a procedure. In theory, I believe, the API used (ODBC or whatever) should be able to translate to the native Informix syntax.