INOUT parameters in SQL
A payment procedure that hands a receipt back through its parameter list.
-- An INOUT parameter carries a result back from a CALL.
CREATE PROCEDURE take_payment(
order_no INTEGER,
amount NUMERIC,
INOUT receipt TEXT DEFAULT NULL
)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO payments (order_id, amount, paid_at)
VALUES (order_no, amount, now());
receipt := 'paid ' || amount || ' against order ' || order_no;
END;
$$;
CALL take_payment(102, 34.95);
How it works
INOUT receipt TEXT DEFAULT NULLis both argument and result.- The body records the payment, then assigns the receipt string.
CALLprints the INOUT values as a result row when they come back.
Keywords and builtins used here
ASBEGINCALLCREATEDEFAULTENDINOUTINSERTINTEGERINTOLANGUAGENULLNUMERICPROCEDURETEXTVALUES
The run, in numbers
- Lines
- 16
- Characters to type
- 371
- Tokens
- 73
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 67 seconds.
Step 2 of 4 in Procedures & transactions, step 10 of 23 in PostgreSQL Stored Procedures.