One feature in SQL Server that we find lots of developers dont know about is row and table constructors. In this quick tip we are going to illustrate how to use the row constructor inside INSERT and SELECT statements.
Some points to note when using the row constructor:
Create a sample database.
Create a sample table.
Simple select statement using the row constructor. Rows are denoted by open and closed brackets(), the fields in the rows are separated by comma’s. At the end I’ve aliased the table constructor and defined the column names.
If you want to explicitly define the column data types then you need to do so as part of the row constructor. Run the following code, you’ll notice that in the first query SQL Server implies the column data types based on the data within the rows. In the second query we’ve explicitly defined the column data types in the first row and can see that these have been applied when creating the #TEMP table.
You can use the row constructor to create the source for an insert statement.
You can also insert DEFAULT and NULL values.
Finally, you can use the row constructor as the candidate for a join.