Friday, June 4, 2010

How to insert mutlitiple rows into a table using insert and select statement?

Please find below how you can insert mutipe rows to a table by uing a select satement .
 for this example let us create a table as below

create table Batting (Player varchar(10), Year int, Team varchar(10), HomeRuns int, primary key(Player,Year))

The row to the above table can be inserted by uing the below sql querry.
 insert into Batting

select 'A',2001,'Red Sox',13 union all
select 'A',2002,'Red Sox',23 union all
select 'A',2003,'Red Sox',19 union all
select 'A',2004,'Red Sox',14 union all
select 'A',2005,'Red Sox',11 union all
select 'B',2001,'Yankees',42 union all
select 'B',2002,'Yankees',39 union all
select 'B',2003,'Yankees',42 union all
select 'B',2004,'Yankees',29 union all
select 'C',2002,'Yankees',2 union all
select 'C',2003,'Yankees',3 union all
select 'C',2004,'Red Sox',6 union all
select 'C',2005,'Red Sox',9