Execute Dynamic SQL commands in SQL Server

In some applications having hard-coded SQL statements is not appealing, because of the dynamic nature of the queries being issued against the database server. Because of this sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can be done quite simply from the application perspective where the statement is built on the fly whether you are using ASP.NET , ColdFusion or any other programming language. But how do you do this from within a SQL Server stored procedure? SQL Server offers a few ways of running a dynamically built SQL statement. These ways are: Writing a query with parameters Using EXEC Using sp_executesql Writing a query with parameters This first approach is pretty straightforward if you only need to pass parameters into the WHERE clause of your SQL statement. Let’s say we need to find all records from the Customers table where City = ‘London’. This can be done easily as the following example shows. ...

Update data in one table with data from another table

Updating Multiple Columns in a Table Using Values from Another Table - A Cross-Platform Approach

In relational database management systems (RDBMS), updating multiple columns in one table with values from another is a common task. Let’s explore how to achieve this in three widely-used systems: SQL Server, MySQL, and PostgreSQL. For this demonstration, we have two tables, TableA and TableB, linked by a foreign key relationship.

Table Structures and Data

TableA:

a b c d
1 x y z
2 a b c
3 t x z

TableB:

a1 b1 c1 d1 e1
1 x1 y1 z1 40
2 a1 b1 c1 50

The goal is to update columns b, c, and d in TableA from the corresponding columns in TableB, based on the foreign key relationship (TableA.a = TableB.a1) and an additional condition (TableB.e1 > 40).

SQL Server

UPDATE TABLEA
SET b = TABLEB.b1, c = TABLEB.c1, d = TABLEB.d1
FROM TABLEA
INNER JOIN TABLEB ON TABLEA.a = TABLEB.a1
WHERE TABLEB.e1 > 40;

Results after the update:

a b c d
1 x y z
2 a1 b1 c1
3 t x z

In SQL Server, the UPDATE...FROM...JOIN syntax allows us to join the tables and specify the columns to be updated based on the given conditions.

MySQL

UPDATE TABLEA
JOIN TABLEB ON TABLEA.a = TABLEB.a1
SET TABLEA.b = TABLEB.b1, TABLEA.c = TABLEB.c1, TABLEA.d = TABLEB.d1
WHERE TABLEB.e1 > 40;

Results after the update:

a b c d
1 x y z
2 a1 b1 c1
3 t x z

MySQL uses a similar approach with the UPDATE...JOIN...SET syntax.

PostgreSQL

UPDATE TABLEA
SET (b, c, d) = (TABLEB.b1, TABLEB.c1, TABLEB.d1)
FROM TABLEB
WHERE TABLEA.a = TABLEB.a1 AND TABLEB.e1 > 40;

Results after the update:

a b c d
1 x y z
2 a1 b1 c1
3 t x z

In PostgreSQL, the UPDATE...SET...FROM...WHERE syntax accomplishes the same task.

By understanding the nuances of each RDBMS syntax, you can seamlessly perform such updates across various database systems.

Comments

Thakur said…
Thanks a lot sir ur coding help me so much
Neeraj Garg said…
Another good link for this :
http://neeraj-garg.blogspot.com/2009/03/how-to-update-values-of-one-column-of.html
This comment has been removed by the author.
This comment has been removed by the author.
RijuPrajul said…
Riju : Thanks a lot....
ivan said…
This comment has been removed by the author.

Popular posts from this blog

Check If Temporary Table Exists

Multiple NULL values in a Unique index in SQL

How To search entire database?