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 2005 Forums
 Transact-SQL (2005)
 inserting records

Author  Topic 

karthikeyan.marlen
Starting Member

16 Posts

Posted - 2011-11-29 : 01:22:55
Hi
I am having the empty table demo2
as

subj1 mark1 subj2 mark2 subj3 mark3

i am having the table demo1 as

sno name subj mark
1 aa tamil 90
1 aa english 80
1 aa science 70
2 bb tamil 100

and i have created a stored procedure to insert records from demo1 to demo2
it (storedprocedure) inserts records as
----------------------------------------------
subj1 mark1 subj2 mark2 subj3 mark3
tamil 90 null null null null
null null english 80 null null
null null null null science 70
---------------------------------------------
but the actual format i want is
subj1 mark1 subj2 mark2 subj3 mark3
tamil 90 english 80 science 70

--------------------------------------------------

the storedprocedure i have created is
-----------------------------------------------------
create procedure inserttable (@no int)
as
begin
declare @sql varchar(max);
declare @cnt int;
set @cnt=1;
set @sql='insert into demo2 '
while(@cnt <= @no)
begin
set @sql=@sql+'(subj'+cast(@cnt as varchar(2))+',mark'+cast(@cnt as varchar(2))+''
set @sql=@sql +' ) select subj,mark from demo1 where sno=@cnt
end
end
-------------------------------------------------------------------
please provide a solution for this,thanks in advance

karthikeyan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-29 : 03:18:46
see

http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sql-programmers
Posting Yak Master

190 Posts

Posted - 2011-11-29 : 04:12:06

I have a blog article to maybe assist you with your question,
http://www.sql-programmers.com/Blog/tabid/153/EntryId/6/Using-PIVOT-and-UNPIVOT.aspx

SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page
   

- Advertisement -