Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I am having a tough time with this UPDATE statement. I am not finding much help on Google about how to UPDATE 2 tables in a SQL command. I know it's not theoretically possible based on what I've read, but I've read it can be done through a transaction. Here is my statement so far, that isn't working. If you can help, please re-write the statement so I can visually see the solution. Thanks!


BEGIN TRANSACTION 

UPDATE [Company] SET [CompanyName] = @CompanyName, [CompanyRegistry] = @CompanyRegistry 
FROM [Company] T1 
WHERE [T1.CompanyID] = @CompanyID 

UPDATE [Users] SET [FirstName] = @FirstName, [LastName] = @LastName, [Address] = @Address, [City] = @City, [State] = @State, [ZipCode] = @ZipCode, [PhoneNumber] = @PhoneNumber, [EMail] = @EMail 
FROM [Users] T2 
WHERE [T2.UsersID] = @UsersID 

COMMIT
Posted

1 solution

Try
SQL
BEGIN TRY
	BEGIN TRANSACTION


		UPDATE [Company] 
		SET [CompanyName] = @CompanyName, 
		[CompanyRegistry] = @CompanyRegistry 
		FROM [Company] T1 
		WHERE [T1.CompanyID] = @CompanyID 
		 
		UPDATE [Users] 
		SET [FirstName] = @FirstName, 
		[LastName] = @LastName, 
		[Address] = @Address, 
		[City] = @City, 
		[State] = @State, 
		[ZipCode] = @ZipCode, 
		[PhoneNumber] = @PhoneNumber, 
		[EMail] = @EMail 
		FROM [Users] T2 
		WHERE [T2.UsersID] = @UsersID 

	COMMIT TRANSACTION
END TRY
BEGIN CATCH
	ROLLBACK TRANSACTION
		--Error Handling
END CATCH


You may find the below article helpful
SQL Server Transactions and Error Handling[^]
 
Share this answer
 
v3
Comments
JasonMacD 13-Dec-12 12:24pm    
Thanks that what it was the @UsersID had no value.
__TR__ 13-Dec-12 12:35pm    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900