DBD :: Oracle - PL / SQL块无法正常工作

时间:2014-08-28 08:08:43

标签: oracle perl plsql dbi

我正在尝试使用DBD::Oracle执行PL / SQL代码。它会在没有任何错误的情况下执行,但表格不会被删除。

以下是代码:

my @tabs = qw!actor address category city country customer film film_features
film_actor film_category film_text inventory language payment rental staff 
store!; 

for my $ts (@tabs){

    my $csr = $ora->prepare(q{
         DECLARE
              var_count INT;
         BEGIN
             SELECT COUNT(*) INTO var_count
             FROM sys.all_tables WHERE OWNER = 'SAKILA' AND table_name = :ts ;
                 if var_count > 0 then
                     EXECUTE IMMEDIATE 'DROP TABLE :ts CASCADE CONSTRAINTS' ;
                 end if;
          END;
    });
    $csr->bind_param(":ts", $ts);
    $csr->execute;

}

2 个答案:

答案 0 :(得分:3)

No.1 - 将表格列表更改为大写

No.2 - 改变:

FROM

EXECUTE IMMEDIATE 'DROP TABLE :ts CASCADE CONSTRAINTS' ;

TO:

EXECUTE IMMEDIATE 'DROP TABLE '||:ts||' CASCADE CONSTRAINTS' ;

以下是完整代码:

my @tabs = qw!ACTOR ADDRESS CATEGORY CITY COUNTRY CUSTOMER FILM FILM_FEATURES FILM_ACTOR FILM_CATEGORY FILM_TEXT INVENTORY LANGUAGE PAYMENT RENTAL STAFF STORE!;
  for my $ts (@tabs){
     chomp $ts;
     my $csr = $ora->prepare(q{ 
        DECLARE
          var_count INT;
        BEGIN
          SELECT COUNT(*) INTO var_count
          FROM sys.all_tables WHERE OWNER = 'SAKILA' AND table_name = :ts ;
            if var_count > 0 then
              EXECUTE IMMEDIATE 'DROP TABLE '||:ts||' CASCADE CONSTRAINTS' ;
            end if;
        END;
     });
$csr->bind_param(":ts", $ts);
$csr->execute;
}

答案 1 :(得分:2)

我会替换

EXECUTE IMMEDIATE 'DROP TABLE :ts CASCADE CONSTRAINTS';

EXECUTE IMMEDIATE 'DROP TABLE '||:ts||' CASCADE CONSTRAINTS';

因为动态sql在将其直接置于动态查询时不会将:ts识别为变量。