我在支持JSON的postgresql中创建了函数。当插入查询中没有bigint数据但它在列check_number中有bigint数据时失败,它工作正常。数据库表中有bigint数据类型。 我在pgsql中的函数如下:
CREATE OR REPLACE FUNCTION transactions.post_transaction(
tran_ json,
detail_ json,
share_tran_ json)
RETURNS bigint[] AS
$BODY$
DECLARE r transactions.tran_type;
DECLARE _tran_id bigint;
DECLARE _tran_ids bigint[];
BEGIN
FOR r IN SELECT * FROM json_populate_recordset(null::transactions.tran_type, $1)
LOOP
INSERT INTO transactions.transactions(
office_id, tran_date, value_date, book_name, collector_id,
total_amount, teller_id, is_auto_payment, is_compounded, is_flagged, instrument_id,
instrument_number, verification_status_id, verified_by_user_id, audit_user_id, collection_sheet_id,
loan_receipt_id, loan_reimburse_id, cash_transfer_id, remittance_id, standing_instruction_id, ibt_tran_id
)
SELECT r.office_id, r.tran_date, r.value_date, r.book_name, r.collector_id,
r.total_amount, r.teller_id, r.is_auto_payment, r.is_compounded, false, r.instrument_id,
r.instrument_number, r.verification_status_id, r.verified_by_user_id, r.teller_id, r.collection_sheet_id,
r.loan_receipt_id, r.loan_reimburse_id, r.cash_transfer_id, r.remittance_id, r.standing_instruction_id, r.ibt_tran_id;
SELECT CURRVAL('transactions.transactions_tran_id_seq') INTO _tran_id;
IF _tran_ids IS NULL THEN
_tran_ids := array[_tran_id];
ELSE
_tran_ids := _tran_ids || _tran_id;
END IF;
INSERT INTO transactions.transaction_details(
tran_id, account_number_id, loan_id, gl_account_id, share_account_id, repository_id,is_interest,
slip_number, check_number, is_loan_collection, debit, credit, statement_reference, audit_user_id, deno_id, ref_dep_ac_id)
SELECT _tran_id, td.account_number_id, td.loan_id, td.gl_account_id, td.share_account_id, td.repository_id, td.is_interest,
td.slip_number, td.check_number, td.is_loan_collection, td.debit, td.credit, td.statement_reference, r.teller_id, td.deno_id, td.ref_dep_ac_id
FROM json_populate_recordset(null::transactions.tran_detail_type, $2) td
WHERE td.office_id = r.office_id;
INSERT INTO transactions.share_transaction(tran_id, share_account_id, kitta_from, kitta_to, audit_user_id)
SELECT tran_id, share_account_id, kitta_from, kitta_to, audit_user_id
FROM json_populate_recordset(null::transactions.share_tran_type, null) s
WHERE s.office_id = r.office_id;
END LOOP;
RETURN _tran_ids;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION transactions.post_transaction(json, json, json)
OWNER TO postgres;}
Json返回如下查询
SELECT * FROM transactions.post_transaction ((('[{ “audit_user_id”:13000057, “BOOK_NAME”: “退出”, “cash_transfer_id”:NULL, “collection_sheet_id”:NULL, “collector_id”:15000089, “flagged_by_user_id”:空, “ibt_tran_id”:空, “INSTRUMENT_ID”:空, “instrument_number”:空, “is_auto_payment”:空, “is_compounded”:空, “is_flagged”:假的, “loan_receipt_id”:空, “loan_reimburse_id”:NULL, “office_id”:3 “REFERENCE_NUMBER”: “8001061713”, “remittance_id”:NULL, “standing_instruction_id”:NULL, “statement_reference”:空, “teller_id”:13000057,“total_amount”:15000,“tran_date”:“2016/4/22 12:00:00”,“tran_id”:0,“value_date”:“2016年4月22日12:00: 00 AM“,”verify_status_id“:1, “verified_by_user_id”:0},{ “audit_user_id”:13000057, “BOOK_NAME”: “退出”, “cash_transfer_id”:NULL, “collection_sheet_id”:NULL, “collector_id”:15000089, “flagged_by_user_id”:空, “ibt_tran_id”:空, “INSTRUMENT_ID”:空, “instrument_number”:空, “is_auto_payment”:空, “is_compounded”:空, “is_flagged”:假的, “loan_receipt_id”:空, “loan_reimburse_id”:空, “office_id”:16, “REFERENCE_NUMBER”: “8001061713”, “remittance_id”:空, “standing_instruction_id”:空, “statement_reference”:空, “teller_id”:13000057,“total_amount”:15000,“tran_date”:“2016/4/22 12:00:00”,“tran_id”:0,“value_date”:“2016年4月22日12:00: 00 AM“,”verify_status_id“:1, “verified_by_user_id”:0}]')),(('[{{“account_duration”:null,“account_number_id”:13019905,“audit_user_id”:13000057,“check_number”:8001061713,“credit”:null, “借记”:15000, “decimal_value”:0, “deno_id”:空, “gl_account_id”:4849, “interest_rate”:空, “is_interest”:假的, “is_loan_collection”:假的, “is_overdraft”:空, “loan_days”:NULL, “loan_id”:NULL, “loan_receipt_type”:NULL, “office_id”:3 “ref_dep_ac_id”:NULL, “repository_id”:NULL, “share_account_id”:NULL, “slip_number”:空, “statement_reference”:“by self”,“tran_id”:0,“transaction_detail_id”:0},{“account_duration”:null,“account_number_id”:null,“audit_user_id”:13000057,“check_number”:null, “信用”:15000, “借记卡”:空, “decimal_value”:0, “deno_id”:空, “gl_account_id”:4797, “interest_rate”:空, “is_interest”:假的, “is_loan_collection”:假的,“is_overdraft “:空值, “loan_days”:NULL, “loan_id”:NULL, “loan_receipt_type”:NULL, “office_id”:3 “ref_dep_ac_id”:NULL, “repository_id”:NULL, “share_account_id”:NULL, “slip_number”:空, “statement_reference”:“by self”,“tran_id”:0,“transaction_detail_id”:0},{“account_duration”:null,“account_number_id”:null,“audit_user_id”:13000057, “check_number”:空, “信用”:空, “借记卡”:15000, “decimal_value”:0, “deno_id”:空, “gl_account_id”:4801, “interest_rate”:空, “is_interest”:假的, “is_loan_collection”:假的, “is_overdraft”:空, “loan_days”:空, “loan_id”:空, “loan_receipt_type”:空, “office_id”:16, “ref_dep_ac_id”:13019905, “repository_id”:空, “share_account_id”:null,“slip_number”:null,“statement_reference”:“by self”,“tran_id”:0,“transaction_detail_id”:0},{“account_duration”:null,“account_number_id”:null, “audit_user_id”:13000057, “check_number”:空, “信用”:15000, “借记卡”:空, “decimal_value”:0, “deno_id”:空, “gl_account_id”:4781, “interest_rate”:空, “is_interest”:假的, “is_loan_collection”:假的, “is_overdraft”:空, “loan_days”:空, “loan_id”:空, “loan_receipt_type”:空, “office_id”:16, “ref_dep_ac_id”:空 ,“repository_id”:79,“share_account_id”:null,“slip_number”:null,“statement_reference”:“by self”,“tran_id”:0,“transaction_detail_id”:0}]')),(('null' )));
答案 0 :(得分:3)
好的问题可能是json_populate_recordset(null::transactions.tran_detail_type, $2)
,因为这是您使用此特定json数据的唯一地方。
所以这意味着在transactions.tran_detail_type
中,check_number
被声明为整数而不是bigint。
如果我做对了,这应该解决它:
ALTER TABLE transactions.tran_detail_type ALTER COLUMN check_number TYPE bigint;