Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Store procedure code as follows;


SQL
 set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[OH_BatchWise_Collection_Report_Presea](@BatchId varchar(10))as
begin

declare @SNo int,
        @stud_name varchar(100),
        @stud_id varchar(100),
		@CrBillNo varchar(20),
		@BillNo varchar(20),
		@Rcptno varchar(20),
		@Rcptdt varchar(20),
		@RcptAmt varchar(20),
        @Chqtype varchar(20),
        @chqnum varchar(20),
        @pendamt varchar(20)   

create table #TempTable(SNo int, Stud_ID varchar(10), Stud_Name varchar(100),
		Rcptno varchar(20),Rcptdt varchar(20), RcptAmt varchar(20), Chqtype varchar(20), chqnum varchar(20),pendamt varchar(20))
  

declare Batchwise cursor for
select s.stud_id, s.stud_name, cr.cr_bill_no from course_registration cr, batch_course_registration bcr, student s
where cr.stud_id = s.stud_id and bcr.cr_bill_no = cr.cr_bill_no and cr.cr_active = 'A' 
and bcr.bcr_batch_id = @BatchId 

SET @SNo = 0
open Batchwise
fetch next from Batchwise  into @stud_id, @stud_name, @CrBillNo
While @@Fetch_status = 0
	 begin

set @BillNo = 0
set @pendamt = 0

select @BillNo = bill_no,@pendamt = bill_pend_amt from bill_file2 where cr_bill_no = @CrBillNo and bill_active = 'A'

declare Batchwise_cur cursor for                   
    select r.rcpt_no, convert(char(12),r.rcpt_dt,106) as Rcptdt, r.rcpt_amt from receipt_file2 r 
    where r.bill_no =  @BillNo

	open Batchwise_cur
	fetch next from Batchwise_cur into @Rcptno, @Rcptdt, @RcptAmt
	while @@Fetch_status = 0
	begin

		set @Chqtype = ''
		set @chqnum= ''

		select @Chqtype = chq_type, @chqnum = chq_num from cheque_file2 where rcpt_no= @Rcptno

     if @Chqtype = 'DEMAND DRAFT'
		set @Chqtype = 'DD'
	 else
		set @Chqtype = @Chqtype
		
		SET @SNo = @SNo + 1

		insert into #TempTable values(@SNo, @stud_id, @stud_name, @Rcptno,@Rcptdt,@RcptAmt,@Chqtype,@chqnum,@pendamt)  --added
    
    fetch next from Batchwise_cur into @Rcptno, @Rcptdt, @RcptAmt
    end
 

    close Batchwise_cur
    deallocate Batchwise_cur

    fetch next from Batchwise into @stud_id, @stud_name, @CrBillNo
    end 


	close Batchwise
	deallocate Batchwise
  
 
SELECT CASE WHEN RowNo =1 THEN CONVERT(VARCHAR(10), sno) ELSE '' END AS SNo, CASE WHEN RowNo =1 THEN CONVERT(VARCHAR(10), stud_id) ELSE '' END AS Student_Id,
       CASE WHEN RowNo =1 THEN [stud_name] ELSE '' END AS [Student_Name],[Rcptno] as Receipt_No, [Rcptdt] as Receipt_Date, [RcptAmt] as Receipt_Amt, [Chqtype] as Payment_Type, [chqnum] as Number,CASE WHEN RowNo =1 THEN CONVERT(VARCHAR(10), pendamt) ELSE '' END AS Pending_Amt
FROM (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY sno ORDER BY sno) AS RowNo
    FROM #TempTable   
) AS T
    end

From my above Store Procedure when i execute output as follows;

OH_BatchWise_Collection_Report_Presea'B8614
1 53321     RAYEES. P	  472	30 Apr 2011 	59000.00   100000.00
2 52162     PATIL AVINASH 187	19 Feb 2011 	59000.00   100000.00
3 52169     BENIN JACOB J 189	19 Feb 2011 	59000.00   100000.00
4 51693     VARUN KUMAR   40	25 Jan 2011 	59000.00			
5 51693     VARUN KUMAR  384	16 Apr 2011 	100000.00			


From the above output, i want the output as follows;
1 53321     RAYEES. P	  472	30 Apr 2011 	59000.00   100000.00
2 52162     PATIL AVINASH 187	19 Feb 2011 	59000.00   100000.00
3 52169     BENIN JACOB J 189	19 Feb 2011 	59000.00   100000.00
4 51693     VARUN KUMAR   40	25 Jan 2011 	59000.00			
                          384	16 Apr 2011 	100000.00	


For that for each and every student have two more records, student id and student name should not repeat.

for that i written a above code as follows;

SQL
SELECT CASE WHEN RowNo =1 THEN CONVERT(VARCHAR(10), sno) ELSE '' END AS SNo, CASE WHEN RowNo =1 THEN CONVERT(VARCHAR(10), stud_id) ELSE '' END AS Student_Id,
       CASE WHEN RowNo =1 THEN [stud_name] ELSE '' END AS [Student_Name],[Rcptno] as Receipt_No, [Rcptdt] as Receipt_Date, [RcptAmt] as Receipt_Amt, [Chqtype] as Payment_Type, [chqnum] as Number,CASE WHEN RowNo =1 THEN CONVERT(VARCHAR(10), pendamt) ELSE '' END AS Pending_Amt
FROM (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY sno ORDER BY sno) AS RowNo
    FROM #TempTable   
) AS T
    end


The above code is Correct? please help me.

Regards,
Narasiman P.
Posted
Updated 28-Apr-13 22:16pm
v2

1 solution

yes the code is right. do u go any error.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900