插入批量语句后删除数据

问题描述 投票:0回答:1

我这样插入是错误的

insert into tab1 ( col1 , col2 , col3) select col1 , col2,col3 from tab2

我有 7600 行效果

我想删除那7600行,如果我进行下面的删除会安全吗?

delete from tab1 where exists  select col1 , col2,col3 from tab2

我问这个是为了知道我是否会丢失 tab1 中的数据。这个查询会删除超过 7600 行吗?如果有通用数据,我会在 isert 中得到唯一的约束,对吗? (是的,两者都有相同的 PK)

sql sybase
1个回答
0
投票

您的

DELETE
语句甚至不是有效的语法,所以我认为它不会执行您想要的操作。即使您修正了语法,您也没有将
tab1
tab2
联系起来。

尝试跑步:

SELECT COUNT(*)
FROM
    tab1
INNER JOIN tab2 ON tab2.col1 = tab1.col1  -- Is this the primary key? If not then you'll need to join on the whole primary key

如果您得到的数字与执行

INSERT
时得到的数字相同,那么您可以执行以下操作:

DELETE T1
FROM
    Tab1 T1
INNER JOIN Tab2 T2 ON T2.col1 = T1.col1

我会将

DELETE
放入事务中并回滚一次以验证计数,然后如果匹配,则运行它并提交。

© www.soinside.com 2019 - 2024. All rights reserved.