SQL Server FUN WITH ROW CONSTRUCTORS

Hi Friends,

I am not lazy today, just physically conservative 🙂 –so a small post and only scripts ! 🙂

— FUN with row constructors

use tempdb 
go 
 
create table amit1 (name varchar(20), age int); 
go 
create table amit2 (name varchar(20), age int); 
go

— Insert multiple records

INSERT INTO amit1 VALUES('Amitabh', 5), ('Abhishek', 6); 
go 
 
SELECT * FROM amit1 
go

–Now try this: does this work?

INSERT INTO amit2 VALUES ((SELECT Name FROM amit1), (SELECT Age FROM amit1)) 
go

–what about this?

INSERT INTO amit2 VALUES ((SELECT TOP(1) Name FROM amit1), (SELECT TOP(1) Age FROM amit1)) 
go
 
select * from amit2
GO

— Have fun with Row constructors

 

 

 
Data Platform Virtual Summit 2020

Subscribe to SQLServerGeeks YouTube channel. If you want more learning content in your inbox, subscribe to SQLServerGeeks Bulletin.

SQLServerGeeks YouTube | SQLServerGeeks Bulletin | SQLServerGeeks Twitter

Amit Bansal

About Amit Bansal

Amit Bansal is always brainstorming around SQL Server. Despite working with SQL since 1997, he is amazed that he keeps learning new things every single day. SQL Server is AB's first love, and his wife does not mind that. He tries to share as much and spreads the SQL goodness. Internals and Performance Tuning excites him, and also gives him sleepless nights at times, simply because he is not a genius, but quite a hard worker and does not give up. It has been a long and exciting journey since 1997, you can read here: http://sqlmaestros.com/amit-bansal/ He is on Twitter: https://www.twitter.com/A_Bansal

View all posts by Amit Bansal →

Leave a Reply

Your email address will not be published. Required fields are marked *