从其他表插入“特定列" [英] Insert into Specific Column from other table

查看:59
本文介绍了从其他表插入“特定列"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将某些值从一个表插入到另一个表中,并想知道如何使其成为可能.

I am trying to insert some of the values from one table to another and wondering how to make it possible.

表A具有4列,其中A_1和A_2在某些行中为空白.

Table A has 4 columns with A_1 and A_2 as blanks in some rows.

表B具有3列,其中B_1和B_2全部填充为 .

Table B has 3 columns with B_1 and B_2 as filled in all of them.

我想将B_1和B_2中的值分别插入A_1和A_2中,行丢失.我确实有相同的 id 可供加入.

I want to insert values from B_1 and B_2 into A_1 and A_2 respectively where the rows are missing. I do have same id available in both of them for joining purpose.

我在考虑以下内容

proc sql;
    insert into A
    (A_1 , A_2)
    as select B_1 , B_2
    from B
    where A_1 = '' and A_2 = ''
;
quit;

推荐答案

我对SAS不熟悉,您没有列出RBDM,但是查询的基本思想是:

I'm not familiar with SAS, and you didn't list your RBDMs, but the basic idea of the query would be:

update tableA
set a_1 = b.b_1 ,
    a_2 = b.b_2
from tableA a
inner join tableB b on a.Id = b.Id
where a.a_1 is null
   and a.a_2 is null

您具有插入语句的开头,但是除非我对您的情况有误解,否则听起来好像您实际上在寻找更新(如果两个表之间都存在ID).

You have the starts of an insert statement, but unless i'm misunderstanding your scenario it sounds like you're actually looking for an update if the IDs exist between both tables.

请注意,这仅在a.a_1和a.a_2都为a的情况下将表a和b连接到"id"字段上,然后用b.b_1更新a.a_1,并用b.b_2更新a.a_2. null -我不确定您是说null还是空字符串.如果您的意思是空字符串,请使用 a.a_1 =''

note that this joins table a and b on an "id" field, then updates a.a_1 with b.b_1, and a.a_2 with b.b_2, only in cases where both a.a_1 and a.a_2 are null - i wasn't sure if you meant null or empty string. If you meant empty string, switch out a.a_1 is null with a.a_1 = ''

这篇关于从其他表插入“特定列"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆