ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

PostgreSQL²éѯ±íÖÐÊÇ·ñ´æÔÚÖµµÄÓÅ»¯
2014-11-24 07:55:46 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:2´Î
Tags£ºPostgreSQL ²éѯ±í ÊÇ·ñ ´æÔÚ ÓÅ»¯

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
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºPostgresÊý×éʹÓà ÏÂһƪ£ºDataGuardÏà¹Ø²ÎÊý

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Spring Boot Java£º (2025-12-26 16:20:19)
¡¤Spring Boot¤ÇHello (2025-12-26 16:20:15)
¡¤Spring ¤Î»ù±¾¤«¤éŒ (2025-12-26 16:20:12)
¡¤C++Ä£°å (template) (2025-12-26 15:49:49)
¡¤C ÓïÑÔÖÐÄ£°åµÄ¼¸ÖÖ (2025-12-26 15:49:47)