Friday, August 5, 2011

Generate Dynamic Table ,Column by Using Cusor (Binging data for Gridview )

CREATE  PROC SP_FINAL_REPORT
(@SessionId int,@ClassId int,@Section varchar(2), 
 @TestTypeId int,@UserId varchar(40),@Criteria int 

/*-------------------------------
Writer :RS Prajapati
Profile:Software Engineer
Date: 05/08/2011
*/
AS BEGIN   
IF(@Criteria=1) 
BEGIN 
------------------------------------   
CREATE TABLE #TMP   
(   
ID INT NULL,SNAME VARCHAR(50) NULL   
)   
INSERT INTO #TMP EXEC sp_getacademicsubject @UserId,@SessionId,@ClassId ---'Devender',2,13   
--SELECT * FROM #TMP   
------------------------------------   
CREATE TABLE #TMP1   
(ROLLNO INT NULL,STUDENT_ID INT NULL,   
 STUDENT_NAME VARCHAR(50) NULL)   
   
DECLARE @SQL NVARCHAR(1000)   
DECLARE @COLS NVARCHAR(1000)    
DECLARE @SNAME VARCHAR(25)   
   
DECLARE CUR_SUB CURSOR FAST_FORWARD FOR   
SELECT SNAME FROM #TMP   
OPEN CUR_SUB   
FETCH NEXT FROM CUR_SUB INTO @SNAME   
 WHILE(@@FETCH_STATUS=0)   
 BEGIN   
  SET @COLS=@SNAME + ' ' + 'VARCHAR(5)'    
  SELECT  @SQL='ALTER TABLE #TMP1 ADD ' + @COLS   
  EXEC(@SQL)   
FETCH NEXT FROM CUR_SUB INTO @SNAME   
END   
 PRINT @sql 
 CLOSE CUR_SUB                                         
 DEALLOCATE CUR_SUB     
-------------------------------------   
DECLARE @SID INT   
DECLARE @LEN INT   
DECLARE @OBT_MARKS VARCHAR(5)   
DECLARE @GRADE VARCHAR(5)   
DECLARE @EVL_TYPE_ID INT   
DECLARE @STUDENTID VARCHAR(10)   
DECLARE @ROLLNO VARCHAR(5)    
DECLARE @STUDENT_NAME VARCHAR(50)   
SET @COLS=NULL   
SET @SQL=NULL   
   
DECLARE MAIN_CURSOR CURSOR FAST_FORWARD FOR   
  select distinct STM.Current_RollNo,SM._studentID,SM._studentname                     
  from tbl_StudentMaster SM INNER JOIN                      
  student_table_sessionwise STM on SM._studentID=STM.Student_Id                     
  where STM.SessionId=@SessionId and STM.Class=@ClassId  and STM.section=@Section                   
     
 OPEN MAIN_CURSOR    
 FETCH NEXT FROM MAIN_CURSOR INTO @ROLLNO,@STUDENTID,@STUDENT_NAME   
 WHILE (@@FETCH_STATUS=0)   
 BEGIN   
      ----------------------------------------------   
      DECLARE @COLL VARCHAR(2000) 
      SET @COLL=''   
      DECLARE EX_CURSOR CURSOR FAST_FORWARD FOR   
      SELECT ID FROM #TMP    
      OPEN EX_CURSOR    
      FETCH NEXT FROM EX_CURSOR INTO @SID   
      WHILE (@@FETCH_STATUS=0)   
      BEGIN   
              
        SELECT @OBT_MARKS=_OBT_MARKS,@GRADE=_GRADE,   
        @EVL_TYPE_ID=_EVL_TYPE_ID FROM    
        tbl_add_Marks_NS_KG WHERE  _academicId=@SessionId AND    
        _test_typeID=@TestTypeId AND _classID=@ClassId  AND    
        _studentId=@STUDENTID AND _subjectId=@SID   
        IF(@EVL_TYPE_ID=1)   
        begin   
        select @COLL=@COLL + ''''+@OBT_MARKS+''''+','   
        end   
        else   
        begin   
        select @COLL=@COLL + ''''+@GRADE+''''+','   
        end   
      
      SET @SID=NULL   
      FETCH NEXT FROM EX_CURSOR INTO @SID   
      END        
       
      SET @LEN=LEN(@COLL)   
      SET @COLL=SUBSTRING(@COLL,1,LEN(@COLL)-1) 
      SET @SQL='insert INTO #TMP1 VALUES('''+@ROLLNO+''','''+@STUDENTID+''','''+@STUDENT_NAME+''','+@COLL+')'   
      PRINT @SQL   
      EXEC(@SQL)   
      CLOSE EX_CURSOR                                         
      DEALLOCATE EX_CURSOR    
      --insert INTO #TMP1 VALUES(@ROLLNO,@STUDENTID,@STUDENT_NAME)   
      ----------------------------------------------   
   SET @ROLLNO=NULL     SET @STUDENTID=NULL             SET @STUDENT_NAME=NULL     
   FETCH NEXT FROM MAIN_CURSOR INTO @ROLLNO,@STUDENTID,@STUDENT_NAME     
   END   
   CLOSE MAIN_CURSOR                                         
   DEALLOCATE MAIN_CURSOR    
    
 SELECT * FROM #TMP1   
END   
END 

No comments:

Post a Comment

Implementing Service Locator (To Resolve Dependency)

using System; /// <summary> /// Summary description for Class1 /// </summary> public class serviceLocator {     public s...