0

here is my serial table.it has more than 1000 records.its with start number and end number.but between numbers not exist. i need to add all number [start/between & end numbers] records in another temp table number by number like below

EXIST TABLE

select concat(CARD_BULK_CODE,start_serial) startserial,concat(CARD_BULK_CODE,end_serial) endserial
from TSR_BULK_CARD_SERIALS
---------------------------
STARTSERIAL ENDSERIAL |
---------------------------
18126944    18126946  |
18141101    18141122  |
15150722    15150729  |
19069303    19069317  |
---------------------------

REQUIRED TABLE

-----------
SERIAL_NO | 
-----------
18126944    
18126945    
18141101    
18141102    
....
-----------

seem its need pl-sql to implement this. please help me to sort out this issue

I tried with below query with the help of dual. but its very slow and not yet got results :-) running more than 1 Hour

select distinct concat(t.CARD_BULK_CODE,t.START_SERIAL)+level-1 SERIAL
from TSR_BULK_CARD_SERIALS t, dual
connect by level-1<=(concat(t.CARD_BULK_CODE,t.END_SERIAL ))-concat(t.CARD_BULK_CODE,t.START_SERIAL) 
order by 1

EDIT :

Dear Alen & Dba.i tried with your ones and below error occured.

DECLARE
     l_st NUMBER;
     l_en NUMBER;
BEGIN
     FOR rec IN (select concat(card_bulk_code, start_serial) startserial,concat(card_bulk_code, end_serial) endserial from tsr_bulk_card_serials)
     LOOP
          l_st := rec.startserial;
          l_en := rec.endserial;
          FOR rec1 IN l_st..l_en
          LOOP
               INSERT INTO temp(serial_no) values(rec1);
          END LOOP;
     END LOOP;
     COMMIT;
END;
Error at line 1
ORA-01426: numeric overflow
ORA-06512: at line 9

Script Terminated on line 1.
Priyan RockZ
  • 1,605
  • 7
  • 40
  • 68

4 Answers4

4

One way to do it without resorting to plsql

WITH ranges AS
(
  SELECT CONCAT(CARD_BULK_CODE, start_serial) startserial,
         CONCAT(CARD_BULK_CODE, end_serial) endserial
    FROM TSR_BULK_CARD_SERIALS
),
numbers(n) AS (
  SELECT 0 n
    FROM dual
UNION ALL
  SELECT n + 1
    FROM numbers
   WHERE n <= 
  (
    SELECT MAX(endserial - startserial)
      FROM ranges
  )
)
SELECT t.startserial + n.n SERIAL_NO
  FROM ranges t JOIN numbers n
    ON n.n <= t.endserial - t.startserial
 ORDER BY SERIAL_NO

Here is SQLFiddle demo

peterm
  • 91,357
  • 15
  • 148
  • 157
1

Try like this,

WITH t(ST, EN) AS
(
     SELECT 18126944,    18126946 FROM dual
     UNION
     SELECT 18141101,    18141122 FROM dual
     UNION
     SELECT 15150722,    15150729 FROM dual
     UNION
     SELECT 19069303 ,   19069317 FROM dual
)
SELECT DISTINCT st + LEVEL -1
FROM   t
CONNECT BY LEVEL <= (SELECT en - st + 1 FROM DUAL)
ORDER BY 1;

/

Try something like this for PL/SQL,

DECLARE
     l_st NUMBER;
     l_en NUMBER;
BEGIN
     FOR rec IN (SELECT * FROM t)
     LOOP
          l_st := rec.st;
          l_en := rec.en;
          FOR rec1 IN l_st..l_en
          LOOP
               INSERT INTO <your_tab>;
          END LOOP;
     END LOOP;
     COMMIT;
END;     
Dba
  • 6,511
  • 1
  • 24
  • 33
1

Just write some PL/SQL - iterate through your table and insert rows in the temp table.

declare
   l_start number;
   l_end number;
begin
   for r_rec in (select to_number(concat(card_bulk_code, start_serial)) startserial
                 ,      to_number(concat(card_bulk_code, end_serial)) endserial
                 from   tsr_bulk_card_serials )
   loop
      l_start := r_rec.startserial;
      l_end := r_rec.endserial;
      for l_i in l_start..l_end loop
         insert into your_temp_table;
      end loop;
   end loop;
end;
Alen Oblak
  • 3,285
  • 13
  • 27
0
DECLARE
   l_st      NUMBER (20);
   l_en      NUMBER (20);
   testnum   NUMBER (4);
BEGIN
   FOR rec IN (SELECT CONCAT (card_bulk_code, start_serial) startserial,CONCAT (card_bulk_code, end_serial) endserial FROM tsr_bulk_card_serials)
   LOOP
      l_st := TO_NUMBER (rec.startserial);
      l_en := TO_NUMBER (rec.endserial);
      testnum := l_en - l_st;
      DBMS_OUTPUT.put_line (l_st);
      DBMS_OUTPUT.put_line (l_en);

      IF l_st < l_en
      THEN
         FOR rec1 IN 0 .. testnum
         LOOP
            l_st := l_st + 1;

            INSERT INTO temp(serial_no) VALUES (l_st);
         END LOOP;
      END IF;
   END LOOP;
   COMMIT;
END;

above code helped me to sorted my issue thanks all :-)

Priyan RockZ
  • 1,605
  • 7
  • 40
  • 68