加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

每个替代方案的T-SQL?

发布时间:2020-12-12 06:47:37 所属栏目:MsSql教程 来源:网络整理
导读:我需要从一个表中获取数据并将其导入另一个表.在伪代码中,类似这样的东西: For Each row in table1If row.personid is in table2 then update table2.rowElse insert row into table2End IfNext 在T-SQL中执行此操作的最佳方法是什么?据我所知,T-SQL不支持F
我需要从一个表中获取数据并将其导入另一个表.在伪代码中,类似这样的东西:
For Each row in table1
If row.personid is in table2 then
   update table2.row
Else
   insert row into table2
End If
Next

在T-SQL中执行此操作的最佳方法是什么?据我所知,T-SQL不支持For Each ..Next,那么我有什么替代方案?

解决方法

在所有条件相同的情况下,基于集合的操作更好.
update t1
set t1.x = t2.x
.
.
.
from table1 t1
inner join table2 t2 on t1.id = t2.t1id

then

insert into table1
select * from table2 t2 where t2.t1id not in (select table1.id from table1 )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读