User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 423,451 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,730 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 1604 | Replies: 5
Reply
Join Date: Sep 2006
Posts: 45
Reputation: Sarah Lee is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Sarah Lee Sarah Lee is offline Offline
Light Poster

Two update statements

  #1  
Feb 6th, 2007
Hi Everybody

How can I use 2 update stmts in a single SP?
I want to update two tables, with different data, but with the same, where clause

Is there any way to do that?

thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Two update statements

  #2  
Feb 6th, 2007
give an example of what you are trying to do
Reply With Quote  
Join Date: Sep 2006
Posts: 45
Reputation: Sarah Lee is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Sarah Lee Sarah Lee is offline Offline
Light Poster

Re: Two update statements

  #3  
Feb 7th, 2007
I splitted the two updates.
I am trying to update one table with new user entered values
It is working fine

The problem now is with the second update, where I am trying to update some file names values into the table through listbox
here i have two listboxes, in listbox1, i have some filenames, which i can add or remove from listbox2
problem is that the file names are not getting updated.

Suppose the user chooses file1, file2, from listbox1, add to lisbox2 and then update, it is not getting updated.
i am inserting the listbox1.item.value along with the contactid.
Public Sub UpdateAccounts(ByVal contactid As Int32)
Dim userBll As UserCreationBLL = New UserCreationBLL()
Dim listitems As Int32 = lstAllAccounts.Items.Count
Dim lstItems As Int32 = lstAccountAccess.Items.Count
If (lstItems > 0) Then
Dim i As Int16
For i = 0 To lstItems - 1
userBll.UpdateAccounts(contactid, Convert.ToInt32(lstAccountAccess.Items(i).Value))
Next i
End If
End Sub

SP for updating is
ALTER PROCEDURE UpdateAccounts
@contactID int,
@accountID int

AS
UPDATE [Contact Account ]
SET [Account ID] = @accountID
WHERE [Contact ID] = @contactID


thanks
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 482
Reputation: pty is on a distinguished road 
Rep Power: 3
Solved Threads: 33
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: Two update statements

  #4  
Feb 7th, 2007
ALTER PROCEDURE UpdateAccounts
@contactID int,
@accountID int
AS
begin transaction
  UPDATE [Contact Account ]
  SET [Account ID] = @accountID
  WHERE [Contact ID] = @contactID

  UPDATE [Other table]
  SET [Account ID] = @accountID
  WHERE [Contact ID] = @contactId
commit transaction
Note to self... pocket cup
Reply With Quote  
Join Date: Sep 2006
Posts: 45
Reputation: Sarah Lee is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Sarah Lee Sarah Lee is offline Offline
Light Poster

Re: Two update statements

  #5  
Feb 7th, 2007
Thanks

But now i am not using two updates in a single one
I separated two updates in 2 diff SP
First one is working

second one for Updating accounts
If i add some files from listbox1 to listbox2, it is getting updated
if i remove some files from listbox2, and then update, it is not updating

how can i check for the removed items in listbox

SP i am using is
ALTER PROCEDURE UpdateAccounts
@contactID int,
@accountID int
AS
INSERT INTO [Contact Account ]([Contact ID],[Account ID])

sELECT @accountid, @contactid
WHERE NOT EXISTS (SELECT 'X' FROM [dbo].[Contact Account ]
WHERE [Account ID] = @accountid AND
[contact id] = @contactid)
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 482
Reputation: pty is on a distinguished road 
Rep Power: 3
Solved Threads: 33
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: Two update statements

  #6  
Feb 8th, 2007
I don't think there is anything wrong with updating two tables using two statements.

If you wrap them in a transaction you will ensure that you don't only update one (ie if one fails neither are committed).
Note to self... pocket cup
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 2:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC