如何为我的数据库中的所有表创建视图

How to Create Views for all Tables in my DB

我已经编写了下面的代码来更正我数据库中的所有 table。 请检查并更正。

Declare
Name Varchar2(100);
A Number:=0;

Cursor C1 Is 
Select Table_Name From Tabs;

Begin
Open C1;
Loop
A:=A+1;
Fetch C1 Into Name;
Exit When C1%Notfound;
Execute Immediate 'Create Or Replace View Tab||A
                  As
                  Select * From Name';

End Loop;

Close C1;

End

请更正我的代码

修复了您的代码:

Declare
   Name Varchar2(100);
   A Number:=0;
   Cursor C1 Is 
      Select Table_Name From Tabs;
Begin
   Open C1;
   Loop
      A:=A+1;
      Fetch C1 Into Name;
      Exit When C1%Notfound;
      Execute Immediate 'Create Or Replace View Tab'||A||'
                        As
                        Select * From "'||Name||'"';
   End Loop;
   Close C1;
End;
/

但我会这样做:

declare
   procedure p_exec(pCMD in varchar2, pPrintOnly varchar2 default 'n') is
   begin
      if pPrintOnly='y' then
         dbms_output.put_line(pCMD);
      else
         execute immediate pCMD;
         dbms_output.put_line(pCMD);
      end if;
   end;
begin
   for r in (Select rownum rn, Table_Name From Tabs) loop
      p_exec(
         utl_lms.format_message(
            'Create Or Replace View Tab%s As Select * From "%s"'
           , to_char(r.rn,'fm9999')
           , r.table_name
         )
         -- ,'y' -- << uncomment to print commands only without execution
      );
   end loop;
end;
/

此论坛不是给人们分配任务的地方,请就您 运行 遇到的问题提出问题,我们愿意为您提供帮助。请下次努力做得更好。

您的立即执行字符串应该是:

Execute Immediate 
  'Create Or Replace View Tab' ||A|| '
   As
   Select * From ' || Name;

别忘了最后 end

之后的 ;