PostgreSQL²éѯ±íÖÐÊÇ·ñ´æÔÚÖµµÄÓÅ»¯
¾³£»áÅöµ½Õâôһ¸öÓ¦ÓãºÍù±íÀï¸üÐÂÊý¾ÝǰÏȲéѯһ±é±»¸üеÄÊý¾Ý´æ²»´æÔÚ¡£Í¨³£ÎÒÃǵÄ×ö·¨ÊÇʹÓÃselect ²éѯ¹ýÂËÒ»±é£¬È»ºóÔÙ¾ö¶¨ÊÇ·ñ¸üУ¬Ôõô¸üС£
ÔÚPG¿âÀ³ýÁËÒÔÉÏ·½·¨Í⣬»¹ÓÐÒ»ÖÖ¸üÄÜÌáÉýÐÔÄܵİ취£¬Ê¹ÓÃperformÀ´´úÌæselect¡£
Example£º
Create or replace function test.insert_exist_test(i_id int,i_info text)
returns void www.2cto.com
as
$BODY$
--author: kenyon
--created:2012_03_05
--purpose:test insert into a table if exists
declare
begin
perform 1 from test.exists_test where id = i_id;
if not found then
insert into test.exists_test(id,info) values (i_id,i_info);
return;
else
update test.exists_test set info=i_info where id=i_id;
return;
end if;
exception when others then
raise exception 'Insert exists_test(id,info) values(%,%) error.',i_id,i_info;
return; www.2cto.com
end;
$BODY$
language plpgsql;
ʹÓãº
select test.insert_exist_test(1,'kevin');
select test.insert_exist_test(2,'BruceLee');
select test.insert_exist_test(3,'Jacky');
select test.insert_exist_test(1,'kenyon');
×÷Õß kenyon