Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server Update Multiple Tables using Inner Joins

Jul 21, 2013
Introduction:

Here I will explain SQL query to update multiple tables in SQL Server using inner joins or update multiple tables in SQL Server with joins.
Description:
In previous posts I explained difference between LEN and DATALENGTH functions in SQL Server, Convert rows to columns without pivot tables in sql server, SQL query to get data between dates and many articles relating to SQL Server. Now I will explain how to write update query with multiple tables in SQL Server.

Generally we don’t have an option to update multiple tables in single statement because of that we need to update tables separately like as shown below

Syntax to update multiple tables


UPDATE t1
SET t1.Name = 'suresh'
FROM Table1 t1, Table2 t2
WHERE t1.id = t2.id
and t1.id = 10

UPDATE t2
SET t2.username = 'dasari'
FROM Table1 t1, Table2 t2
WHERE t1.id = t2.id
and t1.id = 10
Syntax to update multiple tables with inner join


UPDATE t1
SET t1.Name = 'suresh'
FROM Table1 t1 INNER JOIN Table2 t2
and t1.id = 10

UPDATE t2
SET t2.username = 'dasari'
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.id = t2.id
and t1.id = 10

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

4 comments :

Jacob said...

Hi surresh,
Your posts are really helpfull for me.Could you please post an article on 'Online bank transactions using payment gateways' with asp .net?

ravi said...

Hi Suresh,
It seems inner join keyword is missed. Other wise we will get invalid syntax near ON.

UPDATE t1
SET t1.Name = 'suresh'
FROM Table1 t1 inner join Table2 t2
ON t1.id = t2.id
and t1.id = 1

Anonymous said...

hai suresh sir, can we inner join two image feilds from two tables and display it in a repeater
please give me some codes

Anonymous said...

Hey dude ,

I m having one doubt, hope will get response from your end.

Q: In Database A I m hvng table1 with 100 records. Now I m deleting the Database A and again creating freshly with same name. Now my requirement is I want to save (re insert the) 100 records data before deleting the database and restore (re insert into) after creating fresh database.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.