0

I'm working with an oracle database but I don't have much experience with it, in case I need to make a 'backup' of a table column I was wanting to use CONCAT on the sql server but it works on Oracle, no, it gives the error ORA-00909. the query I'm using is:

SELECT Concat ( 'UPDATE HCAlerta SET descricao="', descricao, '" WHERE neoId=', neoid )
FROM   hcalerta --that would give me AS an answer
UPDATE hcalerta
SET    descricao = "Tarefa 000015 - Solicitar Vaga -  - Requisição de Pessoal."
WHERE  neoid = 1071178
4
  • 2
    concat() only accepts two parameters in Oracle. Use || instead Commented Jul 3, 2020 at 17:59
  • tried this way but returned the same error. SELECT CONCAT('UPDATE HCAlerta SET descricao="'|| descricao || '" WHERE neoId=' || neoId) from HCAlerta Commented Jul 3, 2020 at 18:12
  • @Bruno - || instead of concat(), not within it. You've gone from four arguments to one. Commented Jul 3, 2020 at 18:14
  • returned the same error. Erro: ORA-00911: invalid character Commented Jul 3, 2020 at 18:23

2 Answers 2

3

Oracle's concat() function only takes two arguments. You could nest calls but that's messy:

CONCAT(CONCAT(CONCAT('UPDATE HCAlerta SET descricao="', descricao,), '" WHERE neoId='), neoId)

It's simpler to user the concatenation operator:

'UPDATE HCAlerta SET descricao="' || descricao || '" WHERE neoId=' || neoId

But your quotes are wrong; double quotes surround an identifier, like a table or column name, not a value; so you need single quotes - but need to escape them:

'UPDATE HCAlerta SET descricao=''' || descricao || ''' WHERE neoId=' || neoId

In your query that would then be:

select'UPDATE HCAlerta SET descricao=''' || descricao || ''' WHERE neoId=' || neoId
from HCAlerta

db<>fiddle demo (which isn't displaying the multibyte characters properly; but that's an issue with that environment, not the query...).

You could also use the alternative quoting mechanism but that is maybe more confusing in this particular case.

unfortunately returned to me Error: ORA-00923: FROM keyword not found where expected

If you have a LONG column then you could use a PL/SQL block to handle it:

begin
  for r in (
    select neoId, descricao from HCAlerta
  )
  loop
    dbms_output.put_line('UPDATE HCAlerta SET descricao=''' || r.descricao || ''' WHERE neoId=' || r.neoId);
  end loop;
end;
/

... remembering to enable output in your client (e.g. set serveroutput on in SQL*Plus etc.).

db<>fiddle

It might be easier to take a complete copy of the table and restore it if necessary; or export/import, etc.

Sign up to request clarification or add additional context in comments.

5 Comments

returned this error ORA-00932: inconsistent datatypes: expected CHAR got LONG
descricao is data type LONG? Why are you still using LONG instead of CLOB?
it is as text but the system that is compatible with several banks created for example for mysql the field for VARCHAR (8000) but oracle does not allow field greater than 4000 so I have to make this backup to transform this column into CLOB
Which version of Oracle are you on? From 12c you can have larger varchar2 columns. If what you asre doing is converting from LONG to CLOB then it might be simpler to make a copy of the whole table. Also see this if you haven't already.
the client uses version 11g, I managed by giving a select column and saved it in txt, it worked for this moment, thanks
1

Use the || operator:

SELECT 'UPDATE HCAlerta SET descricao="' || descricao || '" WHERE neoId=' || neoId
FROM HCAlerta;

In Oracle, CONCAT() only takes two arguments.

If you want a version that works in both databases, you can use REPLACE():

SELECT REPLACE(REPLACE('UPDATE HCAlerta SET descricao="[descricao]" WHERE neoId=[neoId]',
                       '[neoId]', neoid
                      ), '[descricao]', descricao
              )
FROM HCAlerta;

3 Comments

unfortunately returned to me Error: ORA-00923: FROM keyword not found where expected
@BrunoMello . . . Try it now.
In the replace version, the descricao needs to be in quotes - ... SET descricao=''[descricao]'' WHERE ..., or added during the replacement; don't know how SQL Server handles quotes though. The first version isn't including the actual value from the table - the concatenation operators shouldn't be inside that string?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.