Nirmal KumarC

Nirmal KumarC

  • 1.3k
  • 327
  • 75.7k

how to weekly one time generate Batchid with cursor using?

Sep 19 2014 9:42 AM

I am cursor using weekly one time generate batch id but that query not worked .

MY QUERY:

Declare @cmn_minor_code varchar(50)
Declare @cbm_batch_start_dt datetime
Declare @Batch_id varchar(60)
Declare @strAlphaNumeric varchar (50) 
Declare @intAlpha INT  
DECLARE @EID varchar(50)
 
        --create table #TempTable(alpha varchar(50),intalpha int)
        begin tran
       
        declare Datewise cursor for
       --- FIRST QUERY---
         select cmn_minor_code , convert(char,cbm_batch_start_dt,106) as Course_start_date,cbm_batch_id as Batch_ID
 from CO_BATCH_MASTER where cbm_batch_start_dt between convert(datetime, '01-Sep-2014',6) 
 and convert(datetime, '18-Sep-2014',6)  order by cbm_batch_start_dt 
       
        open Datewise
       
   fetch next from Datewise  into @cmn_minor_code, @cbm_batch_start_dt,@Batch_id
   While @@Fetch_status = 0
   begin

DECLARE @getEID CURSOR
SET @getEID = CURSOR FOR
 ----I WANT PARTICULAR day using weekly one time create EID----
--SECOND QUERY--
select TOP 1 (select EID from BATCHID  where BID=cbm_batch_id AND B_ACTIV<>'D') from CO_BATCH_MASTER 
    where @cbm_batch_start_dt<cbm_batch_start_dt and @cmn_minor_code=cmn_minor_code ORDER BY cbm_batch_start_dt DESC


OPEN @getEID
FETCH NEXT FROM @getEID INTO @EID
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM @getEID INTO @EID
END
CLOSE @getEID
DEALLOCATE @getEID
---Here @strAlphaNumeric Get it EID  after increment EID value this below query---
        SET @strAlphaNumeric=@EID
       
        SET @intAlpha = PATINDEX('%[0-9]%', @strAlphaNumeric)


        select Cast(SUBSTRING(@strAlphaNumeric,@intAlpha, len(@strAlphaNumeric)) as int)+1 as b
        select SUBSTRING(@strAlphaNumeric,1,@intAlpha-1) as a
       
        begin
      insert into #TempTable(alpha, intalpha) 
values((select SUBSTRING(@strAlphaNumeric,1,@intAlpha-1)),(select Cast(SUBSTRING


(@strAlphaNumeric,@intAlpha, len(@strAlphaNumeric)) as int)+1))
    end
   
          select alpha+CAST(INTALPHA AS VARCHAR(50)) as EID from #TempTable


       
         fetch next from Datewise  into @cmn_minor_code, @cbm_batch_start_dt,@Batch_id
        end 
        commit tran
  close Datewise
 deallocate Datewise
 


First Query get it data correctly result is:
cmn_minor_code    Course_start_date        Batch_ID
DME                       1 Sep 2014                   B13739    
GME                       01 Sep 2014                   B13740    
PH1                        15 Sep 2014                   B12869    

The second query i am assign parameter value not get it below query 

select TOP 1 (select EID from BATCHID  where BID=cbm_batch_id AND B_ACTIV<>'D') from CO_BATCH_MASTER where cbm_batch_start_dt<@cbm_batch_start_dt and cmn_minor_code=@cmn_minor_code ORDER BY cbm_batch_start_dt DESC
   

second qurery get not get EID this one main problem,i want that query result compare date with minor code(batch name) wise get EID.

i want  this second query result is below.

EID
 
DME/B107
GME/B1001
PH1/B349

this result come after weekly one time add EID
finally that EID value result is me want

DME/B108
GME/B1002
PH1/B350







Thanks