tags:

views:

152

answers:

3

Hi. 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.

+1  A: 

Why not write whatever logic you need in a stored procedure/package and call that from .NET?

cagcowboy
Thanks for the heads up, but apparently, I cannot utilize a stored proc because there are other sql commands executed and at the same time, some values are generated which are not coming from the database.
Batuta
+3  A: 

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).

Steve Broberg
This worked for me to some degree....But what if the case changes, i.e. inside the CASE WHEN() statement, the values need to pass some more database validations?like:CASE WHEN (SELECT col1 FROM tblC WHERE tblC.id = 1 > 0 AND SELECT col1 FROM tblD WHERE tblD.id = 2 > 0)How will I be able to construct this?
Batuta
I'm having difficulty understanding your new requirement - your WHEN clause above doesn't make sense (you're saying WHEN (SELECT ...), but where is the comparison operation for the results returned by the SELECTS?). Additionally, I'm having a hard time understanding exactly why you need to do all this in the first place. Why the requirement for a single statement? Why can't you use an anonymous PL/SQL block or procedure if the logic is so complex? And what exactly are you trying to do? It seems like one of those problems where you need to take a step back from the tactical problem.
Steve Broberg
basically, in the Case statement you posted, you are comparing 0, 0 to two columns coming from the tableA. I was asking if there is a way that for example, the value for tableA.col1 may be compared into tableC.Col1 and the value for tableA.col2 compared to tableC.Col2 and if both validations are passed, then the value for the column to update is set in the THEN block.As I have mentioned, I cannot use SP on this matter. Kind of hard to explain why I cannot, but definitely, SP is out of the question.
Batuta
You can make the WHEN clause as complex as you wish. I used the (0, 0) = construction to avoid running multiple queries, but if I understand you correctly, you could do what you want by writing WHEN (select col1 from tableA where ...) = (select col1 from table C where ...) AND (select col2 from tableA where ...) = (select col2 from tablcC)
Steve Broberg
A: 

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.

FerranB
stored proc does not fit best on the case I am having. Thanks for the heads up anyway.
Batuta