我有一个db2表 DBTable
带列 A, B, C
(都是varchar类型的),它被链接到一个库的 lib
在SAS中,我用SAS生成一个数据集。ValuesForA
有一列,我想把它的内容写进这一列。A
的 DBTable
附加要求是: B
充满了 ' '
(空白),并将 C
与 (null)
. 所以... DBTable
应该是这样的。
| A | B | C |
======================
| 'x' | ' ' | (null) |
| 'y' | ' ' | (null) |
| 'z' | ' ' | (null) |
我找不到一种方法来实现这一点,因为SAS将空白视为空。
简单的方法是指定 B
作为 " "
只是在这一列中填入 (null)
. 我还尝试使用 nullchar=no
选项,而没有指定 C
:
proc sql;
insert into lib.DBTable
(nullchar=no, A, B)
select
A, " " as B
from ValuesForA;
quit;
然而,这一栏 C
然后也是填空
| A | B | C |
===================
| 'x' | ' ' | ' ' |
| 'y' | ' ' | ' ' |
| 'z' | ' ' | ' ' |
试试这个。
proc sql;
insert into lib.DBTable
select
A, " " as B, null as C
from ValuesForA;
quit;
这给了我你所要求的结果,使用DB2的临时表,有三个VARCHAR列。
我把同样的问题发布在 Community.SAS.com.最后,我使用了s_lassen提出的一个解决方案(你可以看看他的另一个解决方案).我在这里给出了我自己对他的解决方案的描述。
看来,在链接的libraies上插入空白和空值是不可能的。然而,我们可以写一个SAS程序,生成一个sql-statement,我们可以使用pass-through sql在数据库服务器上执行。
这一切都由这个小脚本来完成。
/* *** Create a temporary document with the name "tempsas" *** */
Filename tempsas temp;
/* *** Loop throuhg ValuesForA and write into tempsas. *** */
/* (The resulting tempsas is basically an SQL insert statement. All observations are written in one big values statement) */
Data _null_;
file tempsas;
set ValuesForA end=done;
if _N_=1 then put
'rsubmit;' /
'proc sql;' /
' Connect to DB2(<connect options>);' /
' execute by DB2(' /
' insert into DBTable(A, B, C)' /
' values'
;
put
" ('" a +(-1) "', ' ', null)" @; /* "a" is an observation ValuesForA.a
"+(-1)" removes the trailing blank set by the put statement
"@" writes the next put statement in the same line */
if done then put
/
' );' /
'quit;' /
'endrsubmit;'
;
else put ',';
run;
/* *** Run the code in tempsas *** */
%include tempsas;
它创建了一个文件 "tempsas",在里面写并执行下面的代码。
rsubmit;
proc sql;
Connect to DB2(<connect options>);
execute by DB2(
insert into DBTable(A, B, C)
values
('x', ' ', null),
('y', ' ', null),
('z', ' ', null)
);
quit;
endrsubmit;
我想这个方案只有在没有太多值要插入数据库的情况下才可行。