日期:2014-05-17 浏览次数:20801 次
DECLARE BEGIN create table account_user( userno char(10), username char(12), balance number ); insert into account_user values('1','存款',13000) insert into account_user values('2','支票',100) commit; EXCEPTION WHEN OTHERS THEN ROLLBACK; END;
------解决方案--------------------
--试试
DECLARE BEGIN EXECUTE IMMEDIATE 'create table account_user( userno char(10), username char(12), balance number )' ; EXECUTE IMMEDIATE 'insert into account_user values(''1'',''存款'',13000)'; EXECUTE IMMEDIATE 'insert into account_user values(''2'',''支票'',100)'; commit; EXCEPTION WHEN OTHERS THEN ROLLBACK; END;
------解决方案--------------------
it seems that the create table statement and insert statement cannot co-exist.
because
DECLARE
BEGIN
EXECUTE IMMEDIATE 'create table account_user(
userno char(10),
username char(12),
balance number)';
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
can return successful,
and
DECLARE
BEGIN
insert into account_user values('1','deposit',13000) ;
insert into account_user values('2','cheque',100) ;
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
can return succefully too.
but
DECLARE
BEGIN
EXECUTE IMMEDIATE 'create table account_user(
userno char(10),
username char(12),
balance number)';
insert into account_user values('1','deposit',13000) ;
insert into account_user values('2','cheque',100) ;
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
cannot return successfully.
------解决方案--------------------
DECLARE
BEGIN
EXECUTE IMMEDIATE 'create table account_user(
userno char(10),
username char(12),
balance number
)';
EXECUTE IMMEDIATE 'insert into account_user values(''1'',''存款'',13000)';
EXECUTE IMMEDIATE 'insert into account_user values(''2'',''支票'',100)';
commit;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
这个可以编译成功的
------解决方案--------------------
原因在于 insert into account_user values 这条语句。
sql在执行时要先分析sql语句,
当分析到这条时,因为table account_user还没有创建,
因此DB无法解析这一条语句,因而报错。
可以将这两条insert语句用execute immediate 这种方式执行,
可以绕过db对语句的检查。