我正在尝试使用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;
}
答案 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
识别为变量。