日期:2014-05-17  浏览次数:20740 次

判断一个记录是否存在
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楼的思想严谨,顶.......

楼主可以结贴了.