Updating a table from another table in sql server

LEARN SQL SERVER ONLINE | SQL SERVER TIPS AND TRICKS | SQL SERVER EXAMPLES TSQL | MSSQL | SQL Server 2012 | SQL SERVER 2008 R2 | SQL SERVER 2005 | SQL SERVER 2000 | SQL Server Personal Blog By Devi Prasad Cannot drop the publication because at least one subscription exists for this publication.Drop all subscriptions to the publication before attempting to drop the publication.Results after the update: a b c d ———————————— 1 x y z 2 a1 b1 c1 3 t x z DB2 LUW: –Same as Oracle– UPDATE TABLEA SET (b, c, d) = (SELECT b1, c1, d1 from TABLEB WHERE TABLEB.a1 = TABLEA.a and TABLEB.e1 40); Results after the update: a b c d ———————————— 1 x y z 2 a1 b1 c1 3 t x z NOTE: It is very important to make sure that your where clause for the update statement is correct since that is what identifies the records that the update statement is going to qualify and do the update upon.If it is incorrect, then you can get wrong results.If the proble The parameter must be ‘description’, ‘taskid’, ‘sync_method’, ‘status’, ‘repl_freq’, ‘restricted’, ‘retention’, ‘immediate_sync’, ‘enabled_for_internet’, ‘allow_push’, ‘allow_pull’, ‘allow_anonymous’, or ‘retention’.The value for the @sync_method parameter is not valid.Table Structures and values: Table A has four columns: a, b, c, d (a is the primary key column) Table B has five columns: a1, b1, c1, d1, e1 (a1 and b1 together constitute the primary key for this table) The foreign key relationship between the two tables is based on A.a = B.a1 The data in these 2 tables is as follows: I. Table B a1 b1 c1 d1 e1 1 x1 y1 z1 40 2 a1 b1 c1 50 The requirement is to write a SQL to update columns b, c and d in Table A from the columns b1, c1 and d1 from Table B where-ever the join condition satisfies and e1 40) / Results after the update: a b c d ———————————— 1 x y z 2 a1 b1 c1 3 t x z SQL Server: UPDATE TABLEA SET b = TABLEB.b1, c = TABLEB.c1, d = TABLEB.d1 FROM TABLEA, TABLEB WHERE TABLEA.a = TABLEB.a1 AND TABLEB.e1 40 GO Note: This is an extension in SQL Server i.e.the FROM clause – it does make it simple to understand and is a nice feature.

When simply updating one table based on the rows of another table, improved performance and scalability can be achieved with basic INSERT, UPDATE, and DELETE statements.For this question, you can use the power of relational algebra.The sample code in Listing 1 demonstrates that you can use the FROM clause combined with a JOIN operation to update specified columns from a second table.When you design relational expressions, you have to decide whether you want a single row to match multiple rows (in a one-to-many—1: M—relationship) or you want many rows to match a single row in the joined table (i.e., you want to update all rows).In a 1: M relationship, SQL Server always uses the last row it finds for the update.The reason I am mentioning this is because I have seen people write wrong where clauses and then wondering what went wrong because they specified the correct condition in the SET clause.

You must have an account to comment. Please register or login here!