Update query runs in SQL Server but not in Oracle
-
28-09-2019 - |
Pergunta
I need this update query to run on both SQL Server and Oracle. Our Oracle version is 10.2 if that matters. When I run the query in Oracle I get "ERROR ORA-00933: SQL command not properly ended". What do I need to do to get this to run in Oracle?
UPDATE dbo.tableUpdate
SET fieldA = tt.fieldB
FROM dbo.tableTranslate tt
WHERE
tt.fieldC = dbo.tableUpdate.fieldC
AND
tt.fieldD = dbo.tableUpdate.fieldA
AND
1 = (
SELECT COUNT(tblTrans.fieldD)
FROM dbo.tableTranslate tblTrans
WHERE
tblTrans.fieldC = dbo.tableUpdate.fieldC
AND
tblTrans.fieldD = dbo.tableUpdate.fieldA
)
Solução
The UPDATE...FROM syntax is not valid for Oracle. You will need to use a subquery, like this:
UPDATE dbo.tableUpdate t
SET t.fieldA = (SELECT tt.fieldB
FROM dbo.tableTranslate tt
WHERE tt.fieldC = t.fieldC
AND tt.fieldD = t.fieldA
)
WHERE 1 = (
SELECT COUNT(tblTrans.fieldD)
FROM dbo.tableTranslate tblTrans
WHERE tblTrans.fieldC = t.fieldC
AND tblTrans.fieldD = t.fieldA
)
Outras dicas
The syntax for co-related sub query is a little different in Oracle.
UPDATE dbo.tableUpdate
SET fieldA = (select tt.fieldB
FROM dbo.tableTranslate tt
WHERE
tt.fieldC = dbo.tableUpdate.fieldC
AND
tt.fieldD = dbo.tableUpdate.fieldA)
AND
1 = (
SELECT COUNT(tblTrans.fieldD)
FROM dbo.tableTranslate tblTrans
WHERE
tblTrans.fieldC = dbo.tableUpdate.fieldC
AND
tblTrans.fieldD = dbo.tableUpdate.fieldA)
You will need to rewrite completely your query for Oracle. Some stuff that won't work in Oracle 10.2:
from
clause inupdate
sentence (you will need to write a subselect for this)dbo
schema, unless you actually have a user nameddbo
in oracle owning the table you are trying to update
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow