Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 3523

Sql script code taking minutes to execute

$
0
0

Hi,

 

My Requirement is to retrive data from table , loop through it and adjust the columns based on business logic and output it in sql calculation view  .

 

I wrote the Sql Script in 2 different versions both are taking minutes ,which i don't understand because there are only 15000 records in the table .

 

 

The statement that is Taking more time is 

var_out = select :pernr as PERNR,:cpamt as CPAMT,:curre as CURRE from DUMMY UNIONALLselect * from :var_out;


Here is the Code

 

 

Version 1 -- using cursors 

 

 

/********* Begin Procedure Script ************/

BEGIN

 

 

declare pernr NVARCHAR(8) := ''

declare CPAMT DECIMAL(13,0) := 0;

declare CURRE NVARCHAR(5) := '';

 

declare counter int := 1;

DECLAREcursor c_p0759 for

   select p.pernr, p.subty as citem ,t.CREVI ,t.cplan,p.begda,p.seqnr,

            p.cstat ,p.carea,p.cpamt,p.curre,p.xhour,p.cpnum,p.stkun

            from"HCDCLNT010"."PA0759"as p innerjoin"HCDCLNT010"."T71ADM09"as t

            on p.subty = t.CITEM ;

 

 

 

 

for p0759_curr_row as c_p0759 do

 

  pernr := p0759_curr_row.PERNR ;

  cpamt := p0759_curr_row.cpamt ;

  curre := p0759_curr_row.curre ;

 

  var_out = select :pernr as PERNR,:cpamt as CPAMT,:curre as CURRE from DUMMY UNIONALLselect * from :var_out;

 

 

  endfor;

 

END/********* End Procedure Script ************/

 

version 2 - with out cursor using While

 

/********* Begin Procedure Script ************/

BEGIN

  DECLARE p759_count BIGINT;

  DECLARE rowcount BIGINT := 1 ;

 

comp_records = select ROW_NUMBER() over(orderby (SELECT 1 from dummy)) as rownumber,p.pernr, p.subty as citem ,t.CREVI ,t.cplan,p.begda,p.seqnr,

            p.cstat ,p.carea,p.cpamt,p.curre,p.xhour,p.cpnum,p.stkun

            from"HCDCLNT010"."PA0759"as p innerjoin"HCDCLNT010"."T71ADM09"as t

            on p.subty = t.CITEM ;

 

p758_records = select pernr from"HCDCLNT010"."PA0758";

 

selectcount(*) into p759_count from :comp_records;

 

WHILE rowcount < :p759_count

DO

 

p759_row = select * from :comp_records where rownumber = :rowcount ;

var_out = select pernr,cpamt,curre from :p759_row UNIONALLselect * from :var_out ;

ENDWHILE;

 

 

END/********* End Procedure Script ************/

 

 

 

Thanks,

sree


Viewing all articles
Browse latest Browse all 3523

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>