简体   繁体   English

SQL Server到Oracle查询转换

[英]SQL Server to Oracle query convert

i have a code in sql server like this 我在sql server中有这样的代码

IF EXISTS (SELECT * FROM address WHERE new_add=new_add)
    UPDATE address set old_add=old_add);
ELSE
    UPDATE address set old_add=new_add);

Its working properly in SQL Server. 它在SQL Server中正常工作。 But its not working in oracle. 但是它在oracle中不起作用。 It shows 表明

SP2-0734: unknown command beginning "IF EXISTS ..." - rest of line ignored. 

Plese help me to change this query from SQL to Oracle. 请帮助我将此查询从SQL更改为Oracle。 Thanks 谢谢

SELECT 1 FROM address WHERE new_add=new_add

if @@RowCount > 0
 UPDATE address set old_add=old_add

In both databases, you can write the query as: 在两个数据库中,您都可以将查询编写为:

update address
    set old_add = old_add
    where exists  (SELECT * FROM address WHERE new_add = new_add);

The if is not needed at all. 根本不需要if

However, this query is non-sensical. 但是,此查询是无意义的。 It either sets a column to itself in all rows (if the condition in the where matches at least one row). 它要么在所有行中为其自身设置一列(如果where中的条件至少匹配一行)。 Or, it updates no rows (if the condition in the where has no matching rows). 或者,它不更新任何行(如果where的条件没有匹配的行)。 In either case, nothing really happens. 无论哪种情况,都不会发生。

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

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