Plpgsql错误:尝试插入时缺少表的FROM子句条目

时间:2016-06-23 13:50:38

标签: postgresql plpgsql

每次我尝试在表SaleosOrderDetail中插入内容时,都会出现以下错误:

  

错误:缺少表“salesorderdetail”LINE 1的FROM子句条目:   SELECT CustomerID FROM SalesOrderHeader WHERE SalesOrderDeta ...                                                          QUERY:从SalesOrderHeader中选择CustomerID WHERE SalesOrderDetail.SalesOrderID =   SalesOrderHeader.SalesOrderID语境:PL / pgSQL函数   SQL语句中的new_order_detail()第6行

     

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

     

错误:缺少表“salesorderdetail”SQL的FROM子句条目   state:42P01上下文:PL / pgSQL函数new_order_detail()第6行at   SQL语句

代码是:

CREATE OR REPLACE FUNCTION new_order_detail()
RETURNS trigger AS
$BODY$
DECLARE CustID INT;
BEGIN


    SELECT CustomerID FROM SalesOrderHeader WHERE SalesOrderDetail.SalesOrderID = SalesOrderHeader.SalesOrderID INTO custID;
    UPDATE Customer SET number_of_items = number_of_items + 1 WHERE CustomerID = custID ;



END;
$BODY$ LANGUAGE plpgsql;


DROP TRIGGER IF EXISTS new_order ON SalesOrderDetail;

CREATE TRIGGER new_order
    AFTER INSERT OR UPDATE ON SalesOrderDetail
    FOR EACH ROW EXECUTE PROCEDURE new_order_detail();

2 个答案:

答案 0 :(得分:1)

尝试替换:

<form action="welcome" method="post">
        <input type="text" name="name" />
        <input type="text" name="passcode" />
        <input type="submit" value="submit" />
</form>

by:

   SELECT CustomerID FROM SalesOrderHeader WHERE SalesOrderDetail.SalesOrderID = SalesOrderHeader.SalesOrderID INTO custID;

答案 1 :(得分:1)

您缺少表SalesOrderDetail。可能你想要:

SELECT CustomerID 
    FROM SalesOrderHeader, SalesOrderDetail
   WHERE SalesOrderDetail.SalesOrderID = SalesOrderHeader.SalesOrderID
INTO custID;

这种形式的JOIN已经过时了。请改用现代形式:

SELECT CustomerID 
    FROM SalesOrderHeader
         JOIN SalesOrderDetail
         ON SalesOrderDetail.SalesOrderID = SalesOrderHeader.SalesOrderID
INTO custID;

因为PostgreSQL SQL不区分大小写,所以不建议使用骆驼表示法。改为使用下划线字符:

错误:CustomerID,Good:custmer_id