"Syntax error at or near ' , '" 尝试 SELECT INTO

"Syntax error at or near ' , '" while trying to SELECT INTO

在单个 SELECT 查询中选择多个值并分配给多个变量的查询导致错误。我的 Postgres 版本是 9.5.
查询是:

 SELECT INTO region_id ,doc_type,tax_amt fk_bint_supplier_tax_region_id,chr_supporting_document_type,
dbl_base_currency_client_net-dbl_base_currency_market_fare-dbl_base_currency_cc_charge_collected+
dbl_base_currency_vat_in+dbl_base_currency_cc_charge_collected+(19*(dbl_base_currency_tax))*5/10   
FROM tbl_sales_details   WHERE chr_document_status='N' AND vchr_document_no='INV/47922/01/18'
AND vchr_supporting_document_no='5111143004'

错误是:

ERROR:  syntax error at or near ","
LINE 1:  SELECT INTO region_id ,doc_type,tax_amt fk_bint_supplier_ta...
                               ^

********** 错误 **********

ERROR: syntax error at or near ","
SQL state: 42601

您将 into 放在列列表之后:

SELECT region_id, doc_type,tax_amt fk_bint_supplier_tax_region_id, chr_supporting_document_type,
       (dbl_base_currency_client_net - dbl_base_currency_market_fare - 
        dbl_base_currency_cc_charge_collected + 
        dbl_base_currency_vat_in + dbl_base_currency_cc_charge_collected + 19 * dbl_base_currency_tax
       ) * 5/10   
INTO . . . 
FROM tbl_sales_details
WHERE chr_document_status = 'N' AND
      vchr_document_no = 'INV/47922/01/18' AND
      vchr_supporting_document_no = '5111143004';

我不知道变量名是什么,但是在 INTO 之后,SELECT.

中的每个表达式都必须有一个

错误消息表明您运行 语句是普通的SQL。但它只在 PL/pgSQL 块内才有意义。您明确声明它用于:

assigning to multiple variables

这只在过程语言代码中有意义,因为在普通 SQL 中没有变量赋值。 SELECT INTO inside PL/pgSQL has a different meaning than SQL SELECT INTO - 通常不鼓励使用它。手册:

CREATE TABLE AS is functionally similar to SELECT INTO. CREATE TABLE AS is the recommended syntax, since this form of SELECT INTO is not available in ECPG or PL/pgSQL, because they interpret the INTO clause differently. Furthermore, CREATE TABLE AS offers a superset of the functionality provided by SELECT INTO.

INTO 子句的放置没有任何问题 - 当在 PL/pgSQL 中使用时,就像您标记的那样。

相关:

  • SELECT INTO with more than one attribution