Re: Updating database in a loop
Thank you again.
For future reference, if I want to do a similar thing from a program (not
SQL),
do I need the following 2 statements inside the FOR loop ?
UpdateCmdSQL.Pa rameters("@Mast erAccount").Val ue = MasterAccount
UpdateCmdSQL.Pa rameters("@Acco unt").Value = Account
Also, I should call the Update method from outside the loop, shouldn't I ?
"Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
news:1166128340 .322665.24630@7 3g2000cwn.googl egroups.com...
Thank you again.
For future reference, if I want to do a similar thing from a program (not
SQL),
do I need the following 2 statements inside the FOR loop ?
UpdateCmdSQL.Pa rameters("@Mast erAccount").Val ue = MasterAccount
UpdateCmdSQL.Pa rameters("@Acco unt").Value = Account
Also, I should call the Update method from outside the loop, shouldn't I ?
"Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
news:1166128340 .322665.24630@7 3g2000cwn.googl egroups.com...
fniles wrote:
<snip>
<snip>
>
If this is the case, SQL is your friend (err... mostly): the following
commands will update the master accounts for you (no need looping):
>
<aircode>
Dim SQL As String = "update Position " _
& "set Cash = Cash + b.Total " _
& "from Position as a " _
& "join ( " _
& "select MasterAccount, Total = Sum(Cash) " _
& " from Position where MasterAccount is not null " _
& " group by MasterAccount) as b " _
& "on a.Account = b.MasterAccount "
>
'Assuming an open connection in Con
Dim Cmd As New SqlClient.SqlCo mmand(SQL, Con)
Dim Count As Integer = Cmd.ExecuteNonQ uery()
</aircode>
>
As with the majority of the code posted in foruns, don't run this on
your production data =)
>
HTH.
>
Regards,
>
Branco.
>
<snip>
>Actually, this is what I am trying to do:
>Say I have a table Position like the following:
>Account MasterAccount Cash Profit
>123 999 1000 10
>345 999 2000 20
>999
>I am looping thru this table looking for MasterAccount that is not blank
>(in
>this case Account 123 and 345). For each of those records, I want to
>update
>cash for MasterAccount with cash from this Account. In the example, I
>want
>to update cash in Account 999 with cash from Account 123 and Account 345.
>The result will be for Account 999, cash = 3000.
>Say I have a table Position like the following:
>Account MasterAccount Cash Profit
>123 999 1000 10
>345 999 2000 20
>999
>I am looping thru this table looking for MasterAccount that is not blank
>(in
>this case Account 123 and 345). For each of those records, I want to
>update
>cash for MasterAccount with cash from this Account. In the example, I
>want
>to update cash in Account 999 with cash from Account 123 and Account 345.
>The result will be for Account 999, cash = 3000.
>
If this is the case, SQL is your friend (err... mostly): the following
commands will update the master accounts for you (no need looping):
>
<aircode>
Dim SQL As String = "update Position " _
& "set Cash = Cash + b.Total " _
& "from Position as a " _
& "join ( " _
& "select MasterAccount, Total = Sum(Cash) " _
& " from Position where MasterAccount is not null " _
& " group by MasterAccount) as b " _
& "on a.Account = b.MasterAccount "
>
'Assuming an open connection in Con
Dim Cmd As New SqlClient.SqlCo mmand(SQL, Con)
Dim Count As Integer = Cmd.ExecuteNonQ uery()
</aircode>
>
As with the majority of the code posted in foruns, don't run this on
your production data =)
>
HTH.
>
Regards,
>
Branco.
>
Comment