In this article, we will talk about Row Constructors in SQL Server, which was originally shipped with SQL Server 2008.
Inserting Records to a Table Prior to the Row Constructors Feature
There are many cases were we just want to insert some data to a table. The traditional ways of doing that in SQL Server versions earlier than 2008 were by using separate insert statements for each record to be inserted or by using the UNION ALL clause.
Let’s see an example.
Create Sample Table
CREATE TABLE [dbo].[employee]( [ID] [int] NOT NULL, [Name] [varchar](50) NULL ); GO
Code Example – Using Separate Insert Statements:
insert into dbo.employee(id,name) values (1,'EmpA'); insert into dbo.employee(id,name) values (2,'EmpB'); insert into dbo.employee(id,name) values (3,'EmpC');
Code Example – Using the UNION ALL Clause
insert into dbo.employee(id,name) select 1,'EmpA' UNION ALL select 2,'EmpB' UNION ALL select 3,'EmpC'
Inserting Records to a Table Using the Row Constructors Feature
Row constructors allow us to insert multiple records to a table within a single SQL Statement. To this end, records must be contained in parentheses and be separated with commas.
Let’s continue the example discussed above, but this time, using Row Constructors.
Check out the following code example with Row Constructors:
insert into dbo.employee(id,name) values (1,'EmpA'),(2,'EmpB'),(3,'EmpC'); GO
By using one single SQL statement instead of three we get the same result!
Creating a temporary table using Row Constructors
Now, check this out:
select c.empID,c.empName from (values (1,'EmpA'),(2,'EmpB'),(3,'EmpC')) as c(empID,empName); GO
In the above example by using Row Constructors we created a “temporary table”, defined its values and column names and performed a selection. All were done by using a single SQL Statement! That’s great stuff!
Learn more tips like this!
Enroll to our Online Course!Check our online course titled “Essential SQL Server Development Tips for SQL Developers” (special limited-time discount included in link).
Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
Featured Online Courses:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals (SQL Database for Beginners)
- Essential SQL Server Development Tips for SQL Developers
- The Philosophy and Fundamentals of Computer Programming
- .NET Programming for Beginners: Windows Forms (C#)
- Introduction to SQL Server Machine Learning Services
- Introduction to Azure SQL Database
- SQL Server 2019: What’s New
- Entity Framework: Getting Started (Complete Beginners Guide)
- How to Import and Export Data in SQL Server
- Get Started with SQL Server in 30 Minutes
- A Guide on How to Start and Monetize a Successful Blog
Related SQL Server Development Articles:
- Error converting data type varchar to float
- Row Constructors in SQL Server 2008
- The TIME data type in SQL Server 2008 (and later)
- Error converting data type varchar to numeric
- The set identity_insert Command in SQL Server
- Handling NULL Character x00 when Exporting to File Using BCP
- The Net.Tcp Port Sharing Service service on Local Computer started and then stopped
- …more SQL Server development articles
Subscribe to our newsletter and stay up to date!
Check out our latest software releases!
Check out Artemakis’s eBooks!
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.