Update table with multiple SET arguments [duplicate] - sql

This question already has answers here:
T-SQL conditional UPDATE (v2)
(8 answers)
Closed 8 years ago.
I've been given a list of changes to make to a table. I was wondering how it would be possiblet to make all of the changes in one script...I've tried the following
UPDATE tableA
SET col1 = 'somedata' WHERE col2 = 'somereference'
SET col1 = 'someotherdata' WHERE col2 = 'someotherreference'
SET col1 = 'evenmoredata' WHERE col2 = 'anotherreference'
but this doesn't work. Is there a specific syntax I can use to achieve this, or am I stuck with doing it like this:-
UPDATE tableA
SET col1 = 'somedata' WHERE col2 = 'somereference'
UPDATE tableA
SET col1 = 'someotherdata' WHERE col2 = 'someotherreference'
for each change I want to make?

Use a case statement in the single set statement:
UPDATE tableA
SET col1 = case col2
when 'somereference' then 'somedata'
when 'someotherreference' then 'someotherdata'
when 'anotherreference' then 'evenmoredata'
else col1
end
Its a good idea to put the default in of the original value incase whatever where clause you're using mis-fires (and you should use the where clause anyway otherwise you'll update all rows)

Related

How to update multiple records in one go

I want to update columns of a table whose values are NULL and I want to do this for 5 rows, but I'm getting the error : missing SET keyword
I am running the query in oracle SQL developer
The query I'm using is
UPDATE top(5) table_name
set col1=value1,
col2=value2,
col3=value3 where col1=null;
Second query I used is
UPDATE table_name
set col1=value1,
col2=value2,
col3=value3 where col1=null and rownum<=5;
You can do this in below way:
UPDATE table_name
set (col1,col2,col3) = (select col1,col2,col3 from table_name where col1 is null and rownum<=5)
where col1 is null;

Oracle Trigger Update with Where Clause

Say I have something like this :
UPDATE my_table set col2 = '5' where col1 = '111';
UPDATE my_table set col2 = '5' where col3 = '112';
And now I make a before update trigger and I want to know the columns used in UPDATE statement (i.e col1, col3). In other words, can I see the exact update statement that was used in trigger?. Is this possible ?
Thank you!
No you can't. The trigger has no knowledge of what statement led to its execution.

SQL: Update columns and reference these columns in the same update?

What happens in this case, assuming col1 has initially the value of 10:
UPDATE myTable
SET col1 = 20,
col2 = col1 + 10
Will col2 be 20 or 30 after the update?
Too long for a comment.
It will be 20 according to the rules of standard SQL. The new values do not get committed until the end of the update statement. They are not committed row-by-row or column-by-column. Remember the ACID properties of databases -- all the changes take effect at the same time.
It is possible that some database out there does not behave this way. It is easy enough to check in practice.
The column will show 20
the Update function is running as a bulk.
you can do this quick test:
/* Create Test Table
select
10 as col1,
0 as col2
into TestTable
*/
/* update
update TestTable
set col1 = 20,
col2 = col1 + 10
*/
select * from TestTable

SQL update query

I want to update two fields in one sql query. How do I do that?
update tablename set field1= val1 where id=1
Now I want to update 2 fields as follows: How can I do that?
update tablename set field1 =val1 and set field2=val2 where id=1
Your syntax is almost correct, but you can't use AND.
UPDATE tablename SET field1=var1, field2=var2 WHERE id=1
Or to be safe, I like to write UPDATE statements like this:
UPDATE T
SET
T.Field1 = Value1
,T.Field2 = Value2
-- SELECT *
FROM TableName AS T
WHERE T.ID = 1
This way you can be sure of what you'll be updating.
UPDATE TableName
SET Field1=Value1
,Field2=Value2
WHERE id=id_value
Like the others, but this is how I like to indent and format it, on bigger complex queries, proper formating matters alot!
You almost had it:
update tablename
set field1=val1,
field2=val2
where id=1
UPDATE tablename SET field1 = var1, field2 = var2 WHERE id = 1;
COMMIT;

Help Constructing an Oracle SQL with Condition

I need some help in creating an Oracle SQL which I will execute in .NET.
I need to update a column in a table but the value to update the same would be dependent on two different values. To give an example:
Dim sqlcmd as String
Dim collCmd as Collection
For x = 1 to intCount
sqlcmd = "Update tableA Set col1 = :val1, col2 = :val2 Where...."
collcmd.add(sqlcmd)
SELECT col1, col2
FROM tableA
Where .....
If col1 = 0 and col2 = 0 then
sqlcmd = "Update tableB
Set col1 = :value
Where...."
Else
sqlcmd = "Update tableB
Set col1 = :value
Where.."
End If
collcmd.add(sqlcmd)
Next
'Perform the update with transaction here for the collcmd collection.
Apparently, I need to place the update in one sql where the condition is met. Kindly advise. I cannot do a one time execute non query here since if one of the update fails, then I would need to perform a transaction rollback. I am placing all the update statement in one collection and performing the update in one transaction. But the value for the tableA may be different on the next iteration.
Kindly take note that I cannot place the same inside a stored proc since there are other sql commands which are executed prior to the statements above.
Is there a way to create an SQL where the update would go something like:
sqlcmd = "UPDATE tableB b
IF select a.col1 = 0 and select a.col2 = 0 from tableA a
SET b.col1 = "this value"
ELSE
SET b.col1 = "other value"
WHERE...."
Thanks.
You need to use the CASE expression, like so:
UPDATE tableB
SET col1 =
CASE WHEN (0, 0) = (SELECT col1, col2
FROM tableA
WHERE <tableA constraints>
)
THEN "this value"
ELSE "that value"
END
WHERE <tableB constraints>;
Note also the case is using the (a,b,c) = (select A, B, C ...) syntax, which is a handy, but underused sql feature = you can compare not only single values (scalars) but multiple values (vectors).
Why not write whatever logic you need in a stored procedure/package and call that from .NET?
The best thing to do this is with stored procedures. If you have other sql commands or what ever, then use transactions: run as stored procedures as you want intercalated with data that does not come from database, the commit (or rollback).
If something changes the stored procedures becomes invalid on text-sql does not happens, you have to wait to unit-tests o runtime error. We use generated code that makes very easy to call stored procedures (class.method).
DISCLAIMER: I'm not an stored procedure fanatic. I use Stored Procedures and ORM as best fits on each moment. It depends on the case.