当前位置:  开发笔记 > 数据库 > 正文

使用SAS Proc SQL进行条件插入

如何解决《使用SASProcSQL进行条件插入》经验,为你挑选了1个好方法。

我试图将一个较小的表中的记录添加到一个非常大的表中,如果较小的表中的行的主键值不在较大的表中:

data test;
 Length B C $4;
 infile datalines delimiter=',';
 input a b $ c $;
 datalines;
1000,Test,File
2000,Test,File
3000,Test,File
;

data test2;
 Length B C $4;
 infile datalines delimiter=',';
 input a b $ c $;
 datalines;
1000,Test,File
4000,Test,File
;

proc sql;
insert into test
select * from test2
where a not in (select a from test2);
quit;

然而,这不会将记录插入表中Test.谁能告诉我我做错了什么?最终结果应该是应该将a = 4000的行添加到表中Test.

编辑:

使用where a not in (select a from test)是我最初尝试的,它产生了以下错误:

WARNING: This DELETE/INSERT statement recursively references the target table. A consequence of this is a possible data integrity problem.
ERROR: You cannot reopen WORK.TEST.DATA for update access with member-level control because WORK.TEST.DATA is in use by you in resource
environment SQL.
ERROR: PROC SQL could not undo this statement if an ERROR were to happen as it could not obtain exclusive access to the data set. This
       statement will not execute as the SQL option UNDO_POLICY=REQUIRED is in effect.
224  quit;

谢谢



1> Tom..:

您可以分两步完成此过程.首先创建要插入的记录表,然后插入它们.

proc sql ;
  create table to_add as 
    select * from test2
    where a not in (select a from test)
  ;
  insert into test select * from to_add ;
quit;

或者您可以只更改UNDO_POLICY选项的设置,SAS将允许您在更新TEST时参考TEST.

proc sql undo_policy=none;
  insert into test
    select * from test2
    where a not in (select a from test)
  ;
quit;

推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有