为什么PostgreSQL抛出错误列中存储过程内的select语句不存在。。?
我在PostgreSQL中有一个存储过程,我想在表中进行插入。我从过程中获得了一些参数,并使用它们尝试在其他表上选择其他属性 这是我的存储过程:为什么PostgreSQL抛出错误列中存储过程内的select语句不存在。。?,postgresql,stored-procedures,Postgresql,Stored Procedures,我在PostgreSQL中有一个存储过程,我想在表中进行插入。我从过程中获得了一些参数,并使用它们尝试在其他表上选择其他属性 这是我的存储过程: CREATE OR REPLACE FUNCTION "public"."prc_sales_invoice_header_insert"("customercode" varchar, "sales_note" varchar, "automatic_payment_id" int4, "cash_register_code" varchar,...
CREATE OR REPLACE FUNCTION "public"."prc_sales_invoice_header_insert"("customercode" varchar, "sales_note" varchar, "automatic_payment_id" int4, "cash_register_code" varchar,...etc)
RETURNS "pg_catalog"."void" AS $BODY$
--declaring variables to store data from the tables
DECLARE
salesdate date;
salesdocumentserial varchar;
currencycode varchar;
currencycode2 varchar;
customername varchar;
warehousecode varchar;
......etc.
BEGIN
--getting values from tables and storing to variables
SELECT "name" into customername from public."customers" where "customer_code" = customercode;
SELECT CURRENT_DATE into salesdate;
SELECT max(sales_invoice_header_id) into salesdocumentserial from public."sales_invoice_header";
.....
--inserting values
INSERT INTO public."sales_invoice_header"("sales_date",
"sales_document_serial",
"currency_code",
"currency_code2",
"customer_code",
....
VALUES(
salesdate,
salesdocumentserial,
currencycode,
currencycode2,
customer_code,
.....)
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
当我尝试执行时,它会抛出一个错误,说:
错误:列customer\u code不存在,提示:表sales\u invoice\u标头中有一个名为customer\u code的列,但不能从查询的此部分引用该列
表customers存在,并且有一个名为costomer_code的列,但我不明白为什么它不能引用它
表客户:
-- ----------------------------
-- Table structure for customers
-- ----------------------------
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers" (
"customer_id" int4 NOT NULL DEFAULT nextval('"Customers_CustomerId_seq"'::regclass),
"customer_code" varchar COLLATE "pg_catalog"."default",
"barcode" varchar COLLATE "pg_catalog"."default",
"qr_code" varchar COLLATE "pg_catalog"."default",
"tax_id" varchar COLLATE "pg_catalog"."default",
"business_id" varchar COLLATE "pg_catalog"."default",
"city_id" int8,
"mobile" varchar COLLATE "pg_catalog"."default",
"accounting_number" varchar COLLATE "pg_catalog"."default",
"name" varchar COLLATE "pg_catalog"."default"
);
谁能帮我解决这个问题,我做错了什么?还是这是正确的做事方式
提前谢谢
INSERT INTO public."sales_invoice_header"("sales_date",
"sales_document_serial",
"currency_code",
"currency_code2",
"customer_code",
....
VALUES(
salesdate,
salesdocumentserial,
currencycode,
currencycode2,
customer_code,
在这里,customer_代码不是变量,也不是文字-它是一个列名。然而,您并没有从表中选择它——您试图在值中使用它——将不起作用。任用
insert into ... select ...,customer_code from sales_invoice_header
或
或
编辑:
当一个没有名字的马注意到我的VAR\u customer\u代码时,你的customercode必须是第一个参数吗 请回答您的问题,并将表格CustomerSelect maxsales\u invoice\u header\u id的create table语句添加到salesdocumentserial是生成id的可怕方式。并将当前_日期选择为salesdate;可简化为salesdate:=当前_日期@一匹没有名字的马,我知道关于身份证的事,它不会一直这样,只是一个例子。更新了客户表。很好。应该是customercode,因为这是参数的名称是的,这就是问题所在。需要一双新眼睛:p。非常感谢,两位!
insert into ... values(..., VAR_customer_code)
insert into ... values(..., 'value_customer_code')