Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 insert n number of rows

Author  Topic 

mapradeepkumar
Starting Member

1 Post

Posted - 2008-05-20 : 12:44:04
Hi
I have a table
ID RollNo FeeHead Amount
1 1001 234 1000
2 1001 224 1100
3 1001 234 1200
4 1001 235 1300
5 1002 234 1000
6 1002 224 1100
7 1002 234 1200
8 1002 235 1300
9 1002 11 150
10 1003 234 1000
11 1003 224 1100
12 1003 234 1200
13 1003 235 1300
14 1003 11 150
15 1003 10 250

Now i need total 7 rows per rollno irrespective of number of rows it has
ie total 7 records(record count) should be there
RollNo RecordCount
1001 4
1002 5
1003 6
As 1001 has only 4 record count
so i should be able to add another 3 records to make it 7
the other columns can have null values.
i can do this using a loop but i don't want to use a loop, can any one suggest a way to do this in sql2000 or sql2005

Thanks in Advance

ssunny
Posting Yak Master

133 Posts

Posted - 2008-05-20 : 13:50:52
Just write the insert statements.
Or here's the shortcut if you don't want to copy/paste bunch of insert into's...

Let's call the table as a.

insert into a (ID,RollNo,FeeHead,Amount)
select 5,1001,null,null
union all
select 6,1001,null,null
union all
select 7,1001,null,null


Sunny.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-20 : 14:24:18
quote:
Originally posted by mapradeepkumar

Hi
I have a table
ID RollNo FeeHead Amount
1 1001 234 1000
2 1001 224 1100
3 1001 234 1200
4 1001 235 1300
5 1002 234 1000
6 1002 224 1100
7 1002 234 1200
8 1002 235 1300
9 1002 11 150
10 1003 234 1000
11 1003 224 1100
12 1003 234 1200
13 1003 235 1300
14 1003 11 150
15 1003 10 250

Now i need total 7 rows per rollno irrespective of number of rows it has
ie total 7 records(record count) should be there
RollNo RecordCount
1001 4
1002 5
1003 6
As 1001 has only 4 record count
so i should be able to add another 3 records to make it 7
the other columns can have null values.
i can do this using a loop but i don't want to use a loop, can any one suggest a way to do this in sql2000 or sql2005

Thanks in Advance




Insert into table (RollNo,FeeHead,Amount)
SELECT t.RollNo,NULL,NULL
FROM (SELECT RollNo,COUNT(*) AS Rows FROM table GROUP BY RollNo) t
CROSS JOIN master..spt_values v
WHERE v.type='p'
AND v.number BETWEEN 1 AND 7
AND v.number>t.Rows
Go to Top of Page
   

- Advertisement -