Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / Oracle / Oracle Server / August 2007

Tip: Looking for answers? Try searching our database.

i want to print in single line pl/sql block

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
saiba - 28 Aug 2007 11:18 GMT
how can i get output like this

1 = 1
1+2 =  3
1+2+3 = 6
........
1+2+3+4+5+6+7+8+9+10 = 55;
i have to print same order.

i don't got it but i wrote something see and modify and send reply
---
declare
v number := 0;
v1 number;
begin
 for i in 1..10 loop
       for j in 1..i loop
            v:= v+j;
           dbms_output.put_line(j);
            if j<>i then
            dbms_output.put_line(' + ');
            end if;
    end loop;
        dbms_output.put_line(' = '||v);
               v:=0;
 end loop;
end;
/
Brian Peasland - 28 Aug 2007 14:37 GMT
> declare
> v number := 0;
[quoted text clipped - 4 lines]
>              v:= v+j;
>             dbms_output.put_line(j);

Change this to DBMS_OUTPUT.PUT so that it does not add a line-feed here.

>              if j<>i then
>             dbms_output.put_line(' + ');

Change this to DBMS_OUTPUT.PUT so that it does not add a line-feed here.

>              end if;
>     end loop;
>         dbms_output.put_line(' = '||v);

Here is the end of your line, so you do want the PUT_LINE here.

>                 v:=0;
>   end loop;
> end;
> /

Depending on your version, you may have to do SET SERVEROUTPUT ON in
SQL*Plus before running the above so that you can see the output.

HTH,
Brian

Signature

===================================================================

Brian Peasland
dba@nospam.peasland.net
http://www.peasland.net

Remove the "nospam." from the email address to email me.

"I can give it to you cheap, quick, and good.
Now pick two out of the three" - Unknown

--
Posted via a free Usenet account from http://www.teranews.com

Michel Cadot - 28 Aug 2007 17:51 GMT
| how can i get output like this
|
[quoted text clipped - 24 lines]
| end;
| /

Just for fun:

SQL> col operation format a30 justify right
SQL> col "SUM"     format a5
SQL> with data as ( select level lvl from dual connect by level <= 10 )
 2  select lpad(substr(sys_connect_by_path(level,'+'),2)||' =',30) operation,
 3         ( select to_char(sum(lvl))
 4           from data b
 5           connect by prior lvl = lvl+1
 6           start with b.lvl = a.lvl ) "SUM"
 7  from data a
 8  connect by prior lvl = lvl-1
 9  start with lvl = 1
10  order by lvl
11  /
                    OPERATION SUM
------------------------------ -----
                          1 = 1
                        1+2 = 3
                      1+2+3 = 6
                    1+2+3+4 = 10
                  1+2+3+4+5 = 15
                1+2+3+4+5+6 = 21
              1+2+3+4+5+6+7 = 28
            1+2+3+4+5+6+7+8 = 36
          1+2+3+4+5+6+7+8+9 = 45
       1+2+3+4+5+6+7+8+9+10 = 55

10 rows selected.

Regards
Michel Cadot
Shakespeare - 29 Aug 2007 20:00 GMT
> | how can i get output like this
> |
[quoted text clipped - 58 lines]
> Regards
> Michel Cadot

Michel,

A beauty (as always)

Shakespeare
Michel Cadot - 31 Aug 2007 17:51 GMT
| > | how can i get output like this
| > |
[quoted text clipped - 64 lines]
|
| Shakespeare

Thanks,

Of course, the sum of n first integers is n(n+1)/2:

SQL> with data as ( select level lvl from dual connect by level <= 10 )
 2  select lpad(substr(sys_connect_by_path(level,'+'),2),30) operation, '=' "=",
 3         to_char(lvl*(lvl+1)/2) "SUM"
 4  from data
 5  connect by prior lvl = lvl-1
 6  start with lvl = 1
 7  order by lvl
 8  /
                    OPERATION = SUM
------------------------------ - -----
                            1 = 1
                          1+2 = 3
                        1+2+3 = 6
                      1+2+3+4 = 10
                    1+2+3+4+5 = 15
                  1+2+3+4+5+6 = 21
                1+2+3+4+5+6+7 = 28
              1+2+3+4+5+6+7+8 = 36
            1+2+3+4+5+6+7+8+9 = 45
         1+2+3+4+5+6+7+8+9+10 = 55

10 rows selected.

But it was funnier to let Oracle calculates the sum.

Regards
Michel Cadot
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.