Skip to main content

Update column with data from another table in SQL

 
At different point of time we come up with a situation 
where we need to update a column in a table from another
 table based on some condition.
 
So a Basic Syntax for update is:
UPDATE table
SET column1 = expression1,
    column2 = expression2,
    ...
[WHERE conditions];

For us to update based on another table we will be using the
same syntax for update  applying join with another table from
 where we need the data.


UPDATE t1
  SET t1.column_to_update= t2.column_from_update
  FROM Table1 t1
  INNER JOIN Table2 AS t2
  ON t1.common_column= t2.common_column
 

Comments

Popular posts from this blog

Using SqlDataAdapter to fill DataTable in c#

public DataTable List(string sql)         {             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);             SqlCommand cmd = new SqlCommand(sql, con);             DataTable dt = new DataTable();             SqlDataAdapter da = new SqlDataAdapter(cmd);             try             {                              con.Open();                 da.Fill(dt);             }             finally             {                 con.Close();          ...

Connection String in ASP.NET With SQL Server

Connection string can be placed in web.config file found in root directory of the application from .NET 3.5 onward. connection string can be specified with an xml tag  <connectionStrings>  inside  <configuration>  section of web.config file. < connectionStrings > < add name = "myConnectionString" connectionString = "Data Source=databaseServerName; database=database-name; uid=sqlUserName;password=sqlPassword; Integrated Security =True|false|SSPI (any one options) ; " providerName = " System.Data.SqlClient System.Data.SqlClient" /> </ connectionStrings > We can use local database server of SQL Server by using Data Source= (LocalDb)\MSSQLLocalDB we can attach a local database file to the app_data directory by using the property AttachDBFilename=|DataDirectory|\appDatabaseName.mdf in the connection string Connection String can be ...