Monday, November 21, 2011

Stored Procedure (Create autogenerate column in table & Fetch data)By Cursor--Best Use of cursor

CREATE  PROC SP_FINAL_REPORT_NS_KG  --SP_FINAL_REPORT_NS_KG 2,13,'A',6,'satyam',1   
(@SessionId int,@ClassId int,@Section varchar(2),     
 @TestTypeId int,@UserId varchar(40),@Criteria int     
)      
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(8)       
DECLARE @GRADE VARCHAR(5)       
DECLARE @EVL_TYPE_ID VARCHAR(2)       
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(1000)     
      SET @COLL=''    SET @SQL=''     
      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       
           
            SET @OBT_MARKS=ISNULL(@OBT_MARKS,'0')   
            IF(@OBT_MARKS='0')   
            BEGIN   
            SET @OBT_MARKS=ISNULL(@GRADE,'')   
            END   
      
      
            SET @COLL=@COLL + ''''+@OBT_MARKS+''''+','    
   
            SET @OBT_MARKS=NULL       SET @EVL_TYPE_ID=NULL     
            SET @GRADE=NULL           SET @SID=NULL    
          
      FETCH NEXT FROM EX_CURSOR INTO @SID       
      END            
           
      SET @LEN=LEN(@COLL)       
      SET @COLL=SUBSTRING(@COLL,1,LEN(@COLL)-1)   
      set @ROLLNO=ISNULL(@ROLLNO,'')     
      SET @SQL='insert INTO #TMP1 VALUES('''+@ROLLNO+''','''+@STUDENTID+''','''+@STUDENT_NAME+''','+@COLL+')'       
      EXEC(@SQL)     
           
      CLOSE EX_CURSOR                                             
      DEALLOCATE EX_CURSOR        
    -----------------------------------------------------------------------------       
   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 ORDER BY STUDENT_NAME ASC    
END       
END

Implementing Service Locator (To Resolve Dependency)

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