Plsql 如何使用PL/SQL打印星形三角形

Plsql 如何使用PL/SQL打印星形三角形,plsql,oracle11g,Plsql,Oracle11g,实际上,在PL/SQL中可以创建如下所示的星形三角形吗。我知道,这可以在任何其他编程语言中轻松地完成,比如C、C++、java,但是只需要用SQL或PL/SQL就可以知道这是真的。这是我的家庭作业,我应该使用条件从句IF THEN ELSE,loopsFOR,WHILE * *** ***** ******* ********* *********** ************* 及 试试这个 declare @count int,@num int,@

实际上,在PL/SQL中可以创建如下所示的星形三角形吗。我知道,这可以在任何其他编程语言中轻松地完成,比如C、C++、java,但是只需要用SQL或PL/SQL就可以知道这是真的。这是我的家庭作业,我应该使用条件从句IF THEN ELSE,loopsFOR,WHILE

      *
     ***
    *****
   *******
  *********
 ***********
*************

试试这个

declare @count int,@num int,@num1 int, @space int, @str varchar(50)
set @count = 3 set @num = 1
while(@num<=@count)
begin
  set @num1 = 0 set @space = @count-@num
  while (@num1<@num)
  begin
   if @str is null
    set @str = '* '
   else
    set @str = @str+'* '   set @num1 = @num1+1
  end
print (space(@space)+@str)
set @num = @num+1   set @str = null
end
试试这个

declare @count int,@num int,@num1 int, @space int, @str varchar(50)
set @count = 3 set @num = 1
while(@num<=@count)
begin
  set @num1 = 0 set @space = @count-@num
  while (@num1<@num)
  begin
   if @str is null
    set @str = '* '
   else
    set @str = @str+'* '   set @num1 = @num1+1
  end
print (space(@space)+@str)
set @num = @num+1   set @str = null
end

我发现这个问题很有趣,所以我用PostgreSQL解决了它。不幸的是,重复字符似乎不是真正的标准化功能。以下是用于最常见RDBMS的语法:

PostgreSQL:重复'*',n 甲骨文:rpad,n,'*' MS SQL Server:复制'*',n MySQL:重复'*',n 首先创建一个表格,表格中的数字可以达到你能想象的最大三角形:

create table n10 (n) as
  select 0 union
  select 1 union
  select 2 union
  select 3 union
  select 4 union
  select 5 union
  select 6 union
  select 7 union
  select 8 union
  select 9;

create table n1000 (n) as
  select 100 * a1.n + 10 * a2.n + a3.n
  from n10 a1 cross join n10 a2 cross join n10 a3;
然后,要制作一个大小为5的三角形,请将5替换为0到1000之间的任意数字:

with s (s) as (select 5)
select
  repeat(' ', s - n - 1) ||
  repeat('*', 2 * n + 1) ||
  repeat(' ', s - n - 1)
from n1000 cross join s
where n < s
order by n;

with s (s) as (select 5)
select
  repeat(' ', s - n - 1) ||
  repeat('*', 2 * n + 1) ||
  repeat(' ', s - n - 1)
from n1000 cross join s
where n < s
order by n desc;

我发现这个问题很有趣,所以我用PostgreSQL解决了它。不幸的是,重复字符似乎不是真正的标准化功能。以下是用于最常见RDBMS的语法:

PostgreSQL:重复'*',n 甲骨文:rpad,n,'*' MS SQL Server:复制'*',n MySQL:重复'*',n 首先创建一个表格,表格中的数字可以达到你能想象的最大三角形:

create table n10 (n) as
  select 0 union
  select 1 union
  select 2 union
  select 3 union
  select 4 union
  select 5 union
  select 6 union
  select 7 union
  select 8 union
  select 9;

create table n1000 (n) as
  select 100 * a1.n + 10 * a2.n + a3.n
  from n10 a1 cross join n10 a2 cross join n10 a3;
然后,要制作一个大小为5的三角形,请将5替换为0到1000之间的任意数字:

with s (s) as (select 5)
select
  repeat(' ', s - n - 1) ||
  repeat('*', 2 * n + 1) ||
  repeat(' ', s - n - 1)
from n1000 cross join s
where n < s
order by n;

with s (s) as (select 5)
select
  repeat(' ', s - n - 1) ||
  repeat('*', 2 * n + 1) ||
  repeat(' ', s - n - 1)
from n1000 cross join s
where n < s
order by n desc;
试试这个。 第一个循环将以三角形打印星星,第二个循环将其反转

在PL/SQL中:

在SQL中:

试试这个。 第一个循环将以三角形打印星星,第二个循环将其反转

在PL/SQL中:

在SQL中:


这完全可以在Oracle中的sql中完成,如下所示:

SELECT RPAD(' ', :p_num_triangle_rows - LEVEL) || RPAD('*', LEVEL * 2 -1, '*') || RPAD(' ', :p_num_triangle_rows - LEVEL) triangle
FROM   dual
CONNECT BY LEVEL <= :p_num_triangle_rows
ORDER BY CASE WHEN :p_ascending_or_descending = 'a' THEN LEVEL END ASC,
         CASE WHEN :p_ascending_or_descending = 'd' THEN LEVEL END DESC;
p_num_triangle_rows:=3,p_升序或p_desc:='d':

TRIANGLE
--------------------------------------------------------------------------------
*****
 ***
  *
ETA:这是一个PL/SQL版本,它将完成您所追求的功能:

DECLARE
  PROCEDURE produce_triangle_rows (p_num_triangle_rows IN NUMBER,
                                   p_ascending_or_descending IN VARCHAR2 DEFAULT 'a')
  IS
  BEGIN
    dbms_output.put_line('p_num_triangle_rows = '|| p_num_triangle_rows ||', p_ascending_or_descending = ' || p_ascending_or_descending);
    FOR i IN 1..p_num_triangle_rows
    LOOP
      CASE WHEN p_ascending_or_descending = 'a' THEN
                dbms_output.put_line(RPAD(' ', p_num_triangle_rows - i) || RPAD('*', i * 2 - 1, '*') || RPAD(' ', p_num_triangle_rows - i));
           WHEN p_ascending_or_descending = 'd' THEN
                dbms_output.put_line(RPAD(' ', i - 1) || RPAD('*', 2 * (p_num_triangle_rows - i) + 1, '*') || RPAD(' ', i - 1));
      END CASE;
    END LOOP;
  END produce_triangle_rows;
BEGIN
  produce_triangle_rows(p_num_triangle_rows => 5,
                        p_ascending_or_descending => 'a');

  produce_triangle_rows(p_num_triangle_rows => 3,
                        p_ascending_or_descending => 'd');
END;
/


p_num_triangle_rows = 5, p_ascending_or_descending = a
    *    
   ***   
  *****  
 ******* 
*********

p_num_triangle_rows = 3, p_ascending_or_descending = d
*****
 *** 
  *  

请注意,我将过程包装在一个匿名块中,这样我就可以使用不同的参数调用它。您只需自己创建product\u triangle\u rows过程,然后适当地调用它。

这完全可以在Oracle中的sql中完成,如下所示:

SELECT RPAD(' ', :p_num_triangle_rows - LEVEL) || RPAD('*', LEVEL * 2 -1, '*') || RPAD(' ', :p_num_triangle_rows - LEVEL) triangle
FROM   dual
CONNECT BY LEVEL <= :p_num_triangle_rows
ORDER BY CASE WHEN :p_ascending_or_descending = 'a' THEN LEVEL END ASC,
         CASE WHEN :p_ascending_or_descending = 'd' THEN LEVEL END DESC;
p_num_triangle_rows:=3,p_升序或p_desc:='d':

TRIANGLE
--------------------------------------------------------------------------------
*****
 ***
  *
ETA:这是一个PL/SQL版本,它将完成您所追求的功能:

DECLARE
  PROCEDURE produce_triangle_rows (p_num_triangle_rows IN NUMBER,
                                   p_ascending_or_descending IN VARCHAR2 DEFAULT 'a')
  IS
  BEGIN
    dbms_output.put_line('p_num_triangle_rows = '|| p_num_triangle_rows ||', p_ascending_or_descending = ' || p_ascending_or_descending);
    FOR i IN 1..p_num_triangle_rows
    LOOP
      CASE WHEN p_ascending_or_descending = 'a' THEN
                dbms_output.put_line(RPAD(' ', p_num_triangle_rows - i) || RPAD('*', i * 2 - 1, '*') || RPAD(' ', p_num_triangle_rows - i));
           WHEN p_ascending_or_descending = 'd' THEN
                dbms_output.put_line(RPAD(' ', i - 1) || RPAD('*', 2 * (p_num_triangle_rows - i) + 1, '*') || RPAD(' ', i - 1));
      END CASE;
    END LOOP;
  END produce_triangle_rows;
BEGIN
  produce_triangle_rows(p_num_triangle_rows => 5,
                        p_ascending_or_descending => 'a');

  produce_triangle_rows(p_num_triangle_rows => 3,
                        p_ascending_or_descending => 'd');
END;
/


p_num_triangle_rows = 5, p_ascending_or_descending = a
    *    
   ***   
  *****  
 ******* 
*********

p_num_triangle_rows = 3, p_ascending_or_descending = d
*****
 *** 
  *  

请注意,我将过程包装在一个匿名块中,这样我就可以使用不同的参数调用它。您只需自己创建product_triangle_rows过程,然后适当地调用它。

,第9列:PLS-00103:在预期以下情况之一时遇到符号@:开始函数pragma过程子类型当前光标删除优先存在或您正在使用哪个版本的mssql我正在使用toad for oracleı出现如下错误-->ORA-06550:第1行,第9列:PLS-00103:在预期以下情况之一时遇到符号@:开始函数pragma过程子类型当前光标删除优先存在或您正在使用哪个版本的mssql我正在使用toad for Oracle您正在使用哪个数据库?根据问题标签,MS SQLServer@FabianPijcke但根据错误,,甲骨文。哦,我没有听那个讨论。。。事实上:oYes,这是可能的。你在使用哪个数据库?根据问号,MS SQLServer@FabianPijcke但根据错误,甲骨文。哦,我没有关注那个讨论。。。事实上:是的,这是可能的。