1

I'm trying to update a single column in multiple rows by appending the string '999':

UPDATE integration.ol_orders
SET order_id = ((SELECT order_id
             FROM   integration.ol_orders
             WHERE  status = 2) || '999')
WHERE status = 2

but for whatever reason, I keep getting the error of "ORA-01427: single-row subquery returns more than one row tips".

So, as I iterate my n rows, I'm trying to do:

a1 = a1 || '999'
a2 = a2 || '999'
a3 = a3 || '999'
an = an || '999'

Any suggestions how to go about this?

edit: changed '+' to '||', still no luck

5
  • Execute your subquery (SELECT order_id FROM integration.ol_orders WHERE status = 2) and see how many rows are returned. Commented Feb 6, 2015 at 18:10
  • @mmmmmpie 14 rows are returned Commented Feb 6, 2015 at 18:14
  • 1
    Theres your problem. You are trying to set 1 row equal to the output of 14 rows + '999'. BTW + isn't how you concatenate in oracle its ||. Commented Feb 6, 2015 at 18:15
  • @mmmmmpie how would I be able to adjust that "set 1 row equal to" to "set our sub-queries values to our updated values"? Commented Feb 6, 2015 at 18:20
  • I don't really understand the question but what you are trying is really well explained in @jpw's answer below. Commented Feb 6, 2015 at 18:21

2 Answers 2

5

The subquery looks unnecessary, just doing this should work:

UPDATE integration.ol_orders 
SET order_id = order_id || '999' 
WHERE status = 2

If I remember right Oracle uses || for concatenation. It might be necessary to cast the order_id to a character type if it's an integer, I'm not sure about that and can't test it at the moment. (The conversion should be implicit as stated in a comment, otherwise you can use TO_CHAR() to cast it explicitly).

Make sure to have a backup or copy of the table before running though...

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

4 Comments

I agree, though it would be nice to confirm that order_id is not a number. Certainly antihero989 does treat it that way in the attempt above.
Oracle will implicitly convert order_id to a varchar2 in order to apply the concatenation.
True, though I dislike depending on implicit casting.
@Allan Thanks, wasn't sure about that.
0

We could use CONCAT function in Oracle SQL to tackle the issue and it seems cleaner.

UPDATE integration.ol_orders 
SET order_id = CONCAT (order_id,'999') 
WHERE status = 2;

Just for information:

There are two ways to concatenate Strings in Oracle SQL. Either using CONCAT function or || operator.

CONCAT function allows you to concatenate two strings together

SELECT CONCAT( string1, string2 ) FROM dual;

Since CONCAT function will only allow you to concatenate two values together. If you want to concatenate more values than two, you can nest multiple CONCAT function calls.

SELECT CONCAT(CONCAT('A', 'B'),'C') FROM dual;

An alternative to using the CONCAT function would be to use the || operator

SELECT 'My Name' || 'My Age' FROM dual;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.