简体   繁体   English

查询哪个插入然后更新SQL

[英]Query which inserts then Updates SQL

Is it possible to have an insert statement and once that is complete straight away run an update statement? 是否可以有一条插入语句,一旦完成就立即运行一条更新语句?

So Here are my statements: 所以这是我的声明:

Insert 插入

truncate table table1;
INSERT INTO table2 (name,value, ATY)
SELECT table1.name,table1.Value,table1.ATY
FROM table1;

As soon as that is complete, I need the following update to run: 完成后,我需要运行以下更新:

Update 更新

update table2
   SET value=TRIM( BOTH '+' FROM REPLACE( '+' || value || '+', '++', '+000+' ) )

Is this possible? 这可能吗?

Why not do both in one statement? 为什么不在一项声明中同时使用两者?

truncate table table2;

INSERT INTO table2 (name, value, ATY)
    SELECT t1.name,
           TRIM( BOTH '+' FROM REPLACE( '+' || t1.value || '+', '++', '+000+' ) ),
           t1.ATY
    FROM table1 t1;

Note: I assume that you intend to truncate table2 , not table1 . 注意:我假设您打算截断table2 ,而不是table1 If you truncate table1 , then the insert will not do anything because the table will be empty. 如果截断table1 ,则insert将不执行任何操作,因为该表将为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM