PostgreSQL根據查詢結果執行插入或更新操作

2021-10-24 15:19:45 字數 3995 閱讀 9171

最近需要把老專案中oracle語法改為pg的,其中乙個邏輯為匯入excel資料,根據資料的唯一標識做插入或者更新操作,資料庫中有此資料,則執行更新,反之插入。

oracle**:

declare

v_count number;

begin

select

count(1

)into v_count from uns_author_manage a where a.author_no =

#author_no#;

<

![cdata[

if v_count =

0then

insert

into uns_author_manage

(id,

author_no,

author_type,

project_name,

project_property,

valid,

bid_invitation,

estimate_coverage,

estimate_ppremium,

author_person_name,

author_pseson_job,

operator,

operator_name,

create_date

)values

( seq_uns_author_manage.nextval

,nvl(

#author_no#,'')

,nvl(

#author_type#,'')

,nvl(

#project_name#,'')

,nvl(

#project_property#,'')

,nvl(

,nvl(

#valid#,'')

,nvl(

#bid_invitation#,'')

,nvl(

#estimate_coverage#,'')

,nvl(

#estimate_ppremium#,'')

,nvl(

#author_person_name#,'')

,nvl(

#author_pseson_job#,'')

,nvl(

#userid#,'')

,nvl(

#operator_name#,'')

,sysdate)

;else

update uns_author_manage

set author_type = nvl(

#author_type#,'')

,project_name = nvl(

#project_name#,'')

,project_property = nvl(

#project_property#,'')

,valid = nvl(

#valid#,'')

,bid_invitation = nvl(

#bid_invitation#,'')

,estimate_coverage = nvl(

#estimate_coverage#,'')

,estimate_ppremium = nvl(

#estimate_ppremium#,'')

,author_person_name = nvl(

#author_person_name#,'')

,author_pseson_job = nvl(

#author_pseson_job#,'')

,operator = nvl(

#userid#,'')

,operator_name = nvl(

#operator_name#,'')

,update_date = sysdate

where author_no =

#author_no#;

endif;]

]>

end;

改為pg**:

insert

into uns_author_manage

(author_no,

author_type,

project_name,

project_property,

valid,

bid_invitation,

estimate_coverage,

estimate_ppremium,

author_person_name,

author_pseson_job,

operator,

operator_name,

create_date

)values

(coalesce

(#author_no#)

,coalesce

(#author_type#)

,coalesce

(#project_name#)

,coalesce

(#project_property#)

,coalesce(,

coalesce

(#valid#)

,coalesce

(#bid_invitation#)

,coalesce

(#estimate_coverage#)

,coalesce

(#estimate_ppremium#)

,coalesce

(#author_person_name#)

,coalesce

(#author_pseson_job#)

,coalesce

(#userid#)

,coalesce

(#operator_name#)

,now()

)on conflict(author_no)

doupdate

set

author_type =

coalesce

(#author_type#)

,project_name =

coalesce

(#project_name#)

,project_property =

coalesce

(#project_property#)

coalesce

(,valid =

coalesce

(#valid#)

,bid_invitation =

coalesce

(#bid_invitation#)

,estimate_coverage =

coalesce

(#estimate_coverage#)

,estimate_ppremium =

coalesce

(#estimate_ppremium#)

,author_person_name =

coalesce

(#author_person_name#)

,author_pseson_job =

coalesce

(#author_pseson_job#)

,operator =

coalesce

(#userid#)

,operator_name =

coalesce

(#operator_name#)

,update_date =

now(

);

不過要注意下,被conflict()函式修飾的鍵要具有唯一索引,否則會報錯,使用以下**建立唯一索引:

alter

table uns_author_manage add

constraint id_cons unique

(author_no)

;

jmeter根據查詢資料查詢結果

前兩天試了 一下根據請求查詢出的userid,查詢該userid的資訊。1.新建 1 新建執行緒組 2 新建http請求預設值 3 新建http cookie管理器 4 新建http資訊頭管理器 5 新建登入請求,並新增正規表示式獲取token 2.迴圈控制器 1 新增迴圈控制器,設定迴圈次數 2 ...

mysql根據查詢條件更新資料表

menu templateitem表資料更新 插入郵箱伺服器的修改列929 insert ignore into menu templateitem menu template,menu id select menu template,929 from menu templateitem where...

jmeter連線pg庫,根據查詢結果遍歷請求操作

1新增jdbc連線配置 重要引數說明 mysql庫 如 jdbc mysql localhost 3306 database pg庫如 jdbc postgresql localhost 5432 database oracle庫如 jdbc oracle thin user pass host p...