Solved

adding values

Posted on 2014-02-08
5
195 Views
Last Modified: 2014-02-09
i have a table

month_close
year  number
month number




how to add all years in the statement

declare
y = 2015;
x = 1;
begin
loop 
y <= 2100;
loop
x <= 12;
insert into month_close (year, month, ser_start, close_flag) values (y,x,0,0);
end loop;
end loop;
end;

Open in new window


any adjustment for the above code ?
0
Comment
Question by:NiceMan331
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39845194
pl/sql code fixed:
declare
  _year as number;
  _month as number; 
begin
  _year := 2015;
  while _year <= 2100 
  loop 
     _month := 1;
     while _month <= 12
     loop
        insert into month_close (year, month, ser_start, close_flag) values (_year,_month,0,0);
   end loop;
  end loop;
end;  

Open in new window


you could do all of this without pl/sql, btw ...
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39845195
using 1 single insert:
insert into month_close (year, month, ser_start, close_flag)
with _years as ( select 2014 + level value from dual connect by 2014 + level <= 2100 )
, _months as ( select level value from dual connect by level <= 12)
select _years.value, _months.value, 0, 0
  from _years , _months 

Open in new window

0
 

Author Comment

by:NiceMan331
ID: 39845203
Error report:
ORA-06550: line 2, column 3:
PLS-00103: Encountered the symbol "_" when expecting one of the following:

   begin function package pragma procedure subtype type use
   <an identifier> <a double-quoted delimited-identifier> form
   current cursor
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39845205
sorry, my value.
replace _year, _month, etc by v_year, v_month etc ...
0
 

Author Comment

by:NiceMan331
ID: 39845209
great
thanx
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to connect SQL Server from my Oracle database? 11 132
create a nested synonym 4 28
pl/sql - query very slow 26 74
join a table with user_tab_columns in oracle 3 54
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question