Reputation: 49
trying to learn loops and wanted to output all the rows from this query but the counter works and the variables have no data stored.
I've declared everything and what i think has been assigned to the columns but just not getting the output i need
SET SERVEROUTPUT ON SIZE 1000000;
DECLARE
n_counter NUMBER := 0;
v_site is_spool_dir.site%TYPE;
v_client is_spool_dir.client%TYPE;
v_name IS_PRINTS_NAME.name_comment%TYPE;
v_id is_spool_dir.identification%TYPE;
v_date is_spool_dir.last_print%TYPE;
BEGIN
FOR item IN (
SELECT
s.site
,s.client
,p.name_comment
,s.identification
,to_char(s.last_print, 'DD/MM/RRRR HH24:MM:SS') INTO v_site, v_client, v_name, v_id, v_date
FROM
is_spool_dir s
,is_prints_name p
WHERE
to_char(s.last_print, 'DD/MM/YYYY') like '18/01/2019'
and s.site = '7'
and s.client = 'BREV'
and s.identification like 'BREZ%'
and p.id = s.report_id
ORDER BY
to_char(s.last_print, 'DD/MM/YYYY'))
LOOP
n_counter := n_counter + 1;
DBMS_OUTPUT.PUT_LINE(n_counter);
DBMS_OUTPUT.PUT_LINE(v_site ||','|| v_client ||','|| v_name ||','|| v_id ||','|| v_date);
IF n_counter = 1000 THEN
EXIT;
END IF;
END LOOP;
END;
/
OUTPUT
1
,,,,
2
,,,,
3
,,,,
4
,,,,
5
,,,,
6
,,,,
7
,,,,
8
,,,,
9
,,,,
10
,,,,
11
,,,,
12
,,,,
13
,,,,
14
,,,,
15
,,,,
16
,,,,
17
,,,,
18
,,,,
19
,,,,
20
,,,,
21
,,,,
22
,,,,
23
,,,,
24
,,,,
25
,,,,
26
,,,,
27
,,,,
28
,,,,
29
,,,,
30 etc
Upvotes: 0
Views: 47
Reputation: 1372
you can use cursor with pollowing way :
FOR CUR IN ( SELECT
s.site
,s.client
,p.name_comment
,s.identification
,to_char(s.last_print, 'DD/MM/RRRR HH24:MM:SS')
FROM
is_spool_dir s
,is_prints_name p
WHERE
to_char(s.last_print, 'DD/MM/YYYY') like '18/01/2019'
and s.site = '7'
and s.client = 'BREV'
and s.identification like 'BREZ%'
and p.id = s.report_id
ORDER BY
to_char(s.last_print, 'DD/MM/YYYY'))
LOOP
NULL;
--use CUR.site HERE FOR YOUR OPERATIONS ...
END LOOP;
Upvotes: -1
Reputation: 1790
When you declare a cursor for your loop you do not use an into clause.
If you really want to put them into variables you can do an assignment in your loop like this...
v_site := item.site;
Otherwise just use the item variable you have declared in your loop as below
SET SERVEROUTPUT ON SIZE 1000000;
DECLARE
n_counter NUMBER := 0;
BEGIN
FOR item IN (
SELECT
s.site
,s.client
,p.name_comment
,s.identification
,to_char(s.last_print, 'DD/MM/RRRR HH24:MM:SS')
FROM
is_spool_dir s
,is_prints_name p
WHERE
to_char(s.last_print, 'DD/MM/YYYY') like '18/01/2019'
and s.site = '7'
and s.client = 'BREV'
and s.identification like 'BREZ%'
and p.id = s.report_id
ORDER BY
to_char(s.last_print, 'DD/MM/YYYY'))
LOOP
n_counter := n_counter + 1;
DBMS_OUTPUT.PUT_LINE(n_counter);
DBMS_OUTPUT.PUT_LINE(item.v_site ||','|| item.v_client ||','|| item.v_name ||','|| item.v_id ||','|| item.v_date);
IF n_counter = 1000 THEN
EXIT;
END IF;
END LOOP;
END;
Upvotes: 3