SQL Server SAS query issue on external DBMS TABLE where Column Name has space

jhiyze9q  于 2023-04-04  发布在  其他
关注(0)|答案(1)|浏览(205)

Through SAS/ACCESS, I can successfully run data steps querying external DBMS tables. E.g.,

Data OutTable;
 Set ExternalDBMS.Table1;
 Where Var1 ='abc';
Run;

However, when column name has space, it caused a problem even I used ''n. One example as shown below:

Data OutTable;
     Set ExternalDBMS.Table1;
     Where 'Var 2'n ='abc';
    Run;

ERROR: CLI open cursor error: [SAS][ODBC SQL Server Wire Protocol driver][Microsoft SQL Server]Incorrect syntax near the keyword 'Function'.

Further try with SAS Option validvarname=v7 to standardize the var names with spaces still caused same error.

After using SAS Option sastrace=',,,d' I found that SAS/ACCESS submitted statement to SQL server like this:

SELECT   Var 1, .....
FROM  schema1.Table1
WHERE  (Var 1 ='abc' );

Apparently the code above would cause error in SQL server side because the Var 1 was neither quoted nor bracketed.

One way to fix it is using explicit pass-through query. I'm just wondering if there's any other ways to solve this problem too. Thanks in advance!

sdnqo3pr

sdnqo3pr1#

when using an explicit pass-through query, put a set of square brackets around the variable name. This would be similar to how you'd write your code in SSMS.

SELECT [Var 1], ... FROM schema1.Table1 WHERE ([Var 1] ='abc' );

相关问题