存储过程使用DDL语句

-- procedure 中使用 DDL 语句需要将DML封装存入string中,否则会出现异常
-- http://www.cnblogs.com/cici-new/p/4029895.html
/*Error: PLS-00103: 出现符号 "CREATE"在需要下列之一时:
( begin
case declare exit for goto if loop mod null pragma raise
return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge*/
-- 最后要将表名称提取出成为变量名称
Create Or Replace Procedure modify_table_structure(tab_name In Varchar2) As
  pstring_droptable Varchar2(2000);
  pstring_createtab Varchar2(2000);
  pstring_deletetab Varchar2(2000);
  pstring_altertab  Varchar2(2000);
  pstring_inserttab Varchar2(2000);
  v_count Integer;
  
Begin 
  pstring_droptable := 'drop table '||table_name||'_back';
  pstring_createtab := 'create table '||table_name||'_back as select * from'||table_name;
  pstring_deletetab := 'delete from '||table_name;
  pstring_altertab := 'alter table'||table_name|| 'modify sell_money number (10,5)';
  pstring_inserttab := 
   'insert into '||table_name|| ' (id, busiman_name, sell_date, product_type, sell_money)
   select  id, busiman_name, sell_date, product_type, sell_money
   from  '||table_name||'_BACK' ;
  
  Select Count(*) Into v_count From User_Tables 
  Where table_name = 'table_name'||'_back';
  dbms_output.put_line('v_count 1 ' ||v_count);
  If (v_count > 0) Then
    Execute Immediate pstring_droptable;
    dbms_output.put_line('v_count 2'||v_count);
    End If;
    dbms_output.put_line(pstring_droptable);
    dbms_output.put_line(pstring_createtable);
    dbms_output.put_line(Pstring_deletetable);
    dbms_output.put_line(Pstring_altertable);
    dbms_output.put_line(pstring_inserttable);
    
   Execute Immediate pstring_createtable;
   Execute Immediate pstring_deletetable;
   Execute Immediate pstring_altertable;
   Execute Immediate pstring_inserttable;
   Execute Immediate pstring_droptable;
   
   End; 
  
  
  call Modify_Table_Structure('tab_name');
  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值