简体   繁体   中英

issue with Biguqery Stored procedure

create or replace procedure Practise.sp_test()
OPTIONS(strict_mode=false)
BEGIN
DECLARE v_product_code int64;
DECLARE v_price int64;

    set v_product_code = (SELECT product_code FROM `virendra101987.Practise.product` where v_product_code =01);
    set v_price = (SELECT price FROM `virendra101987.Practise.product` where v_product_code =01);
    EXECUTE IMMEDIATE v_product_code;
    EXECUTE IMMEDIATE v_price;

    insert into `Practise.sales1`(order_date,product_code,quantity_order,sales_price)
    values (current_date(),v_product_code,1,v_price);

    END


   CALL `virendra101987.Practise.sp_test`();

Why am I getting this error?

ERROR Query error: Cannot coerce expression v_product_code to type STRING at [virendra101987.Practise.sp_test:7:23]

The error message arises from the EXECUTE IMMEDIATE expecting a string. However in looking at what you're trying to achieve I don't believe it is necessary. The SET operator will execute the SQL statements for you and does not require any further action to evaluate. So you should be able to pass them into your insert statement without issue.

For example:

create or replace procedure Practise.sp_test()
OPTIONS(strict_mode=false)
BEGIN
DECLARE v_product_code int64;
DECLARE v_price int64;

    set v_product_code = (SELECT product_code FROM `virendra101987.Practise.product` where v_product_code =01);
    set v_price = (SELECT price FROM `virendra101987.Practise.product` where v_product_code =01);


    insert into `Practise.sales1`(order_date,product_code,quantity_order,sales_price)
    values (current_date(),v_product_code,1,v_price);

    END;


   CALL `virendra101987.Practise.sp_test`();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM