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

【ALLLLLLLLLLLLLLLLLLLL】判断一个记录是否存在
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 代码单独执行是可以的。

PS:MS-SQL板块的兄弟们,让我到这里问。


------解决方案--------------------
你这个语法不行..
SQL code
DECLARE 
v_num NUMBER 
BEGIN 
  select Count(*) INTO v_num from t_gpslatest where f_code='12';
  IF  v_num!=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 = '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;
END;

------解决方案--------------------
探讨
你这个语法不行..

SQL code

DECLARE
v_num NUMBER
BEGIN
select Count(*) INTO v_num from t_gpslatest where f_code='12';
IF v_num!=0 THEN
update t_gpslatest set f_style='99',f_longtitude='……

------解决方案--------------------
oracle 10g中MERGE有如下改动:
1、UPDATE或INSERT子句是可选的
2、UPDATE和INSERT子句可以加WHERE子句
3、ON条件中使用堂过滤谓词来INSERT所有的行到目标表中,不需要连接源表和目标表
4、UPDATE子句后面可以跟DELETE子句来去除一些不需要的行
--示例
MERGE INTO products p
USING new products np
ON (p.product_id = np. Product_id)
WHERE MATCHED THEN 
UPDATE
SET p. product_name = np. Product_name,
p.category = np.category
DELETE WHERE (p.category = ‘ELECTRNCS’)
WHEN NOT MATCHED THEN 
INSERT
VALUES (np. Product_id,np. Product_name,np.category)
------解决方案--------------------
1楼回答的很好了,楼主就用1楼的答案撒。