创建触发器时出现语法错误

时间:2012-10-30 22:10:34

标签: php postgresql triggers postgresql-9.1 upsert

我知道这个问题已经以各种形式被抛到这里,但我已经完成了所有这些并且无法解决我的问题。我不明白为什么我一直在这个脚本上得到这个语言错误。我按照我在这里看到的两个不同的例子来接近它,但似乎一遍又一遍地给我同样的错误。我真的很难让这个触发器功能工作,请在这里感谢PostgreSQL大师的任何积极帮助。

   <?php
      include ("connection.php");
    $query = 'select * from fieldtally order by pipeno asc'; 
$result = pg_query($db_handle,$query); 
while ($row = pg_fetch_row($result))
{
    // Creates Arrays to use in dropdowns
    $pipeno_array[] = $row[0];
   } 
 // This function creates dropdowns that can be used in your forms
function dropdown($field_name, $num){
    // Creates the Dropdown
    echo "<select name=\"".$field_name."\" id=\"".$field_name.$num."\"\n";
    echo "<option value=\"\"> --- Select --- </option>\n";
    // Chooses which array to use for Dropdown options
   global $pipeno_array;
     $name_array = ($field_name == 'pipeno') ? $pipeno_array : $wthick;
    // Creates the Dropdown options based off the array above
    foreach($name_array as $k){
        echo "<option value=\"$k\">$k</option> \n"; }
    // Ends the Dropdown
    echo "</select>\n";
}
?>

<html>
<head><title>UG Pipeline Fiedl Data Capture</title></head>
<body>
<form action="fieldtally.php" method="post">
<table width="800" cellpadding= "10" cellspacing="1" border="1">
<tr align="center" align="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
<h3>Input Field Tally Information</h3>
Select Wall Thickness:<select name="wthick" id="wthick">
   <option value=""> --Select-- </option> 
  <option value="9.80">  9.80  </option>
  <option value="13.50"> 13.50 </option>
  <option value="15.90"> 15.90 </option>
  </Select> 
Select Pipe No:<?php dropdown('pipeno', 1); ?>   Select Pipe Manufacturer:<select name="manufacturer" id="manufacturer">
 <!-- #7.1 -->
  <option value=""> --Select-- </option> 
  <option value="KWH Pipe">KWH Pipe </option>
  <option value="Atlas Tube">Atlas Tube</option>
  <option value="Imperial Steel">Imperial Steel</option>
  <option value="Lakeside">Lakeside</option>
  <option value="Spiralco">Spiralco</option>
  <option value="Muri metals">Muri metals</option>
  <option value="WestLake">WestLake</option>
  <option value="Westman Steel">Westman Steel</option>
     <option value="Lockerbie & Hole">Lockerbie & Hole</option>
      <option value="Nardei">Nardei</option>
  </Select><br /><br />  
Joint No: <input type="text" name="djointno"> Input measured Length: <input type="text" name="measuredlength"><br><br>
Input Serial No: <input type="text" name="serialno"><br><br> 
<input type="Submit" name="submit" value="Submit">
</td></tr></table></form><p></p>
</form>
</body>
</html>
  

字段tally.php脚本如下所示

<?php
 $PGHOST = "localhost:25376";
 $PGDATABASE = "Pipeline";
 $PGUSER = "postgres";
 $PGPASSWORD = "Casa2009";
 $PGPORT = 5432;
 $db_handle = pg_connect("dbname=$PGDATABASE user=$PGUSER
 password=$PGPASSWORD");
 if ($db_handle) {
 echo 'Connection attempt succeeded.';
 } else {
 echo 'Connection attempt failed.';
 }
 $pipeno = pg_escape_string( $_POST['pipeno']);  
 $wthick = pg_escape_string($_POST['wthick']);
 $djointno = pg_escape_string($_POST['djointno']);
 $manufacturer = pg_escape_string($_POST['manufacturer']);
 $measuredlength = pg_escape_string($_POST['measuredlength']);
 $serialno = pg_escape_string($_POST['serialno']); 
  $query = "CREATE FUNCTION upsert (sql_update TEXT, sql_insert TEXT)
                 RETURNS VOID
                 LANGUAGE plpgsql
            AS $$
            BEGIN
               LOOP
                  EXECUTE sql_update;
                  IF FOUND THEN
                     RETURN;
                  END IF;
                  BEGIN
                     EXECUTE sql_insert;
                     RETURN;
                     EXCEPTION WHEN unique_violation THEN
                  END;
              END LOOP;
            END;
            $$";

   $result = pg_query($query);
 if (!$result) {
 $errormessage = pg_last_error();
 $message = "Error with query: " . $errormessage;
 }
 $message = sprintf ("These values were inserted into the pipeline database - %s %s %s %s %s %s",$wthick,$pipeno,$manufacturer,$djointno,$measuredlength,$serialno);
 pg_close();
?>

1 个答案:

答案 0 :(得分:1)

这两个陈述都很乱。您需要阅读the PL/PgSQL trigger documentation,包括那里给出的示例,以了解语法。

基本上,你首先:

CREATE OR REPLACE FUNCTION func_name() RETURNS trigger AS $$
BEGIN
   -- Trigger body here
END;
$$ LANGUAGE plpgsql;

然后在表格上使用该触发器功能:

CREATE TRIGGER trigger_name
BEFORE INSERT OR UPDATE OR DELETE ON tablename
FOR EACH STATEMENT EXECUTE PROCEDURE func_name();

此外,代码中的触发器主体将永远不会工作。 PostgreSQL不支持ON DUPLICATE KEY UPDATE。这是一个MySQL主义。您正在寻找MERGEUPSERT类似行为,遗憾的是,在PostgreSQL中很难做到这一点。请参阅this question