首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

判断一个记要是否存在

2012-08-19 
判断一个记录是否存在SQL codeif exists( select 1 from t_gpslatest where f_code12)(update t_gpslat

判断一个记录是否存在

SQL code
if exists( select 1 from t_gpslatest where f_code='12')(update t_gpslatest set f_style='99',f_longtitude='88',f_latitude='77',f_direction='66',f_speed='55',f_time=sysdate where f_code = '11')else(insert into t_gpslatest(f_code,f_style,f_longtitude,f_latitude,f_direction,f_speed,f_time)values('11','22222','333333','44444','55555','66666',sysdate))


就是判断f_code = 12 的记录是否存在,如果存在就更新,如果不存在就插入。

上面的代码在Oracle数据库中怎么不行?
update 和 insert into。还都要加上括号,要是不加上,就报ORA-00933:SQL命令未正确结束。

update和Insert into 代码单独执行是可以的。

[解决办法]
Oracle 的你去Oracle专区问这样会好些吧
[解决办法]
探讨
Oracle 的你去Oracle专区问这样会好些吧

[解决办法]
oracle好像没有exists写法,mysql有,你select看看有没有记录不就可以了
[解决办法]
--ORACLE 
try
SQL code
int v_id; select 1 into v_id from t_gpslatest where f_code='12'; if v_d=1 then    update t_gpslatest set f_style='99',f_longtitude='88',f_latitude='77',f_direction='66',f_speed='55',f_time=sysdate where f_code = '11';    else        insert into t_gpslatest(f_code,f_style,f_longtitude,f_latitude,f_direction,f_speed,f_time)values('11','22222','333333','44444','55555','66666',sysdate);    end if
[解决办法]
探讨
Oracle 的你去Oracle专区问这样会好些吧

[解决办法]
SQL code
--你的值都写错了,不是12吗,怎么又成了11了declare     rec int := 0;begin    select count(1) into rec from t_gpslatest where f_code='12';    if rec>0 then        update t_gpslatest set f_style='99',f_longtitude='88',f_latitude='77'            ,f_direction='66',f_speed='55',f_time=sysdate             where f_code = '12';    else        insert into t_gpslatest(f_code,f_style,f_longtitude,f_latitude,f_direction,f_speed,f_time)            values('12','22222','333333','44444','55555','66666',sysdate);    end if;     commit;    exception     when others then        rollback;        dbms_output.put_line('error');end;/
[解决办法]
取值错了?
[解决办法]
正在学习Oracle
[解决办法]
SQL code
declare i number;begin select count(*) into i from t_gpslatest where f_code='12'; if i>0 then  update t_gpslatest      set f_style='99',f_longtitude='88',f_latitude='77',f_direction='66',f_speed='55',f_time=sysdate   where f_code = '12'; else   insert into t_gpslatest(f_code,f_style,f_longtitude,f_latitude,f_direction,f_speed,f_time)            values('11','22222','333333','44444','55555','66666',sysdate); end if;end;
[解决办法]
4楼的当f_code='12'的记录不存在时会有异常, 所以依这样的代码, 不会有新记录插入
9楼的答案也对
6楼的思想严谨,顶.......

楼主可以结贴了.

热点排行