您的位置:首页 > 数据库 > Oracle

利用Ant向Oracle中插入数据

2011-11-07 21:18 190 查看
[1] 去掉脚本中的包含commit、“setserverout off”、“set serverout on”、“set escape on”,和 “exec”的语句

[2] 去掉“/”

[3] 语句分隔符最好使用 “分隔符” + “换行”,如:

ALTER TABLE xxx ADD (temp_advtoolbar CLOB)!

UPDATE xxx SET temp_advtoolbar = advtoolbar!
[4] 插入配置数据前需先判断数据是否存在

当我们要在WCMConfig表中插入一条配置数据的时候,需要先判断该配置参数是否存在,注意该代码是以“end;!”结尾,如:

declare v_count number(10);

begin
SELECT count(*) into v_count FROM xxx WHERE CKey = 'KMS_UPLOAD_FILE_MAX_SIZE' ;

if(v_count<=0 or v_count is null) then
insert into xxx(configid,ctype,ckey,cvalue,cdesc) select max(configid)+1,12,'KMS_UPLOAD_FILE_MAX_SIZE','6291456','批量上传时可以上传的最大文件大小,单位为k' from xxx;

update yyyset nextid=0 where tablename='xxx';
end if;

end;!
[5] 表上增加列需要先判断列是否存在

当我们要在xxx表上增加LEAFFLAG列时,需要先判断xxx表是否已经存在LEAFFLAG列,注意该代码是以“end;!”结尾,具体代码如下:

--增加是否允许创建子场景(即是否为叶子节点)的字段 2011.10.12 by liwei

declare v_count number(10);
begin

SELECT count(*) into v_count FROM cols
WHERE table_name = 'xxx' and column_name='LEAFFLAG' ;

if(v_count<=0 or v_count is null) then
execute immediate('alter table xxx add LeafFlag number default 0 not null');

end if;
end;!

注意:

当我们需要使用begin end语句块的时候,begin语句块中只能使用DML(数据操作语言,如:insert、delete、update和select),如增加配置参数的代码:

declare v_count number(10);

begin

SELECT count(*) into v_count FROM xxx WHERE CKey = 'KMS_UPLOAD_FILE_MAX_SIZE' ;

if(v_count<=0 or v_count is null) then

insert into xxx(configid,ctype,ckey,cvalue,cdesc) select max(configid)+1,12,'KMS_UPLOAD_FILE_MAX_SIZE','6291456','批量上传时可以上传的最大文件大小,单位为k' from wcmconfig;

update yyy set nextid=0 where tablename='xxx';

end if;

end;!

当我们在增加列的时候,可能会使用到alter操作来给一个表添加一列,这时候我们需要使用动态sql(也就是execute immediate)来执行。因为begin end 语句块中只能执行DML语言,如果要执行DDL(alter、create等)语言,需要使用动态sql。如:

declare v_count number(10);

begin

SELECT count(*) into v_count FROM cols

WHERE table_name = 'xxx' and column_name='LEAFFLAG' ;

if(v_count<=0 or v_count is null) then

execute immediate('alter table xxx add LeafFlag number default 0 not null');

end if;

end;!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: