Table"skytf.tmp_basic_info" Column | Type | Modifiers --------------+--------------------------------+----------------------------------------- user_id | integer | not null account | character varying(32) | not null nickname | character varying(64) | comlum1 | smallint | default1 comlum2 | character varying(20) | default '1991-01-01'::character varying comlum3 | character varying(20) | comlum4 | character varying(40) | comlum5 | character varying(20) | comlum6 | character varying(32) | default '中国'::character varying comlum7 | character varying(20) | comlum8 | character varying(16) | comlum9 | character varying(128) | comlum10 | integer | default0 comlum11 | integer | default0 comlum12 | timestamp(0) without time zone | comlum13 | integer | default86 Indexes: "tmp_basic_info_pkey" PRIMARY KEY, btree (user_id) "tbl_mpc_user_info_username_key" UNIQUE, btree (account) "tmp_basic_info_comlum2" btree (comlum2) "tmp_basic_info_comlum8_index" btree (comlum8) "tmp_basic_info_nickname_index" btree (nickname) "tmp_basic_info_comlum7_index" btree (comlum7) "tmp_basic_info_comlum12_idx" btree (comlum12) "tmp_basic_info_comlum1_index" btree (comlum1, comlum8, comlum2, comlum6, comlum4) "tmp_basic_info_comlum9" btree (comlum9) "tmp_basic_info_userid_index" btree (user_id)
由于业务需要,需要将 nickname 的值全表更新成 account。
创建临时表
1 2 3 4
createtable tmp_nickname asselect user_id,account,nickname from tmp_basic_info where nickname like'%蓝雪%'; createuniqueindex idx_user_id on tmp_nickname using btree (user_id); altertable tmp_nickname addcolumn flag char(1); update tmp_nickname set flag='N';
CREATEORREPLACEFUNCTION skytf.func_setnickname() RETURNSnumericAS $BODY$ DECLARE rec RECORD; i integer; BEGIN i:=0; FOR rec IN SELECT user_id,accountFROM skytf.tmp_nickname where flag ='N'limit10000LOOP --记录不符合导入条件1的号码 PERFORM 1FROM skytf.tmp_basic_info WHERE user_id= rec.user_id ; IF FOUND THEN UPDATE skytf.tmp_basic_info SET nickname=rec.account WHERE user_id = rec.user_id; update tmp_nickname set flag='Y'where user_id=rec.user_id; ENDIF; i:= i+1; RAISE NOTICE ' here is %', i; ENDLOOP; return 0; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE COST 100;