SQL函数接受多个输入参数并返回一个表

时间:2018-11-10 20:34:13

标签: sql function

我必须在PGADMIN 4中用SQL编写一个函数,该函数将多个值作为输入并必须返回包含其他信息的表。因此,我尝试了以下代码,但我只是不知道如何使其工作...:

CREATE OR REPLACE FUNCTION public.take_customer_info(varchar, 
varchar,varchar,varchar,date,smallint,boolean,                                           
varchar,varchar,bigint,bigint,varchar,varchar,smallint,bigint,date,                                          
smallint,boolean,smallint,smallint,smallint)

returns table(
          customer_id smallint, 
          email varchar,  
          address varchar, 
          order_id smallint, 
          order_card bigint,
          total smallint)
AS $info$
DECLARE 
order_id smallint;
address varchar;
total smallint;
begin
    SELECT MAX(order_id)
    FROM the_order as order_id;
    address =($13|| ' ' || $11 || ', '|| $12|| ', ' || $8);
    total = $19 * $21;
CASE WHEN customer.email != $1 and product.product_id = $20 then
    insert into public.customer values($1,$2,$3,$4,$5,$6);
    insert into public.shipping_address 
    values($7,$8,$9,$10,$11,$12,$13,$6,$14);
    insert into public.credit_card values($15,$16,$17,$18,$3,$4,$6);
    insert into public.item_ordered values ($20,$19,$21,default,default);
    select currval('public.item_ordered_order_id_sql') into order_id;
    insert into public.the_order values (order_id,$14,default,$15,$6);
    update public.product set product.stock_quantity = 
    product.stock_quantity-$19 where product.product_id = $6;

    WHEN customer.email=$1 THEN
    RAISE EXCEPTION 'Nonexistent email --> %', $1
        USING HINT = 'Please check your user email';
    WHEN product.product_id =! $20 then
    RAISE EXCEPTION 'Nonexistent ID --> %', $20
        USING HINT = 'Please check your product ID';
    END
RETURN QUERY (SELECT customer.customer_id, customer.email, address, order_id, 
the_order.cc, total FROM customer,the_order WHERE customer.customer_id = 
the_order.customer_id);
END;
$info$ language plpgsql;'

0 个答案:

没有答案