在postgresql表中创建插入,更新规则

时间:2012-10-26 21:01:55

标签: sql postgresql

我正在尝试创建一个带有插入和更新规则的postgresql表,这些规则可以使用户自动插入信息或更新数据库表上的现有信息。当我尝试在数据库中运行SQL查询以创建表时,我不断收到此错误。我试图遵循PostgreSQL 9.1中的文档步骤。它一直给我一个主键的表达式错误,指出我需要重写它。请有人看看这个脚本并帮助我。感谢您最宝贵的贡献!

  

这是我的SQL脚本

 CREATE TABLE fieldtally1
  (fieldtally1_id serial NOT NULL,
  pipeno character varying,
  wthick real,
  heatno1 character varying(32),
  pipeno2 character varying(32),
  heatno2 character varying(32),
  Djointno character varying(32),
  ContractorNo character varying(32),
  measuredlength double precision,
  serialno character varying(32),
  CoatingType character varying(32),
  coatingno character varying(32),
  mnfcno character varying(32),
  FactoryLength double precision,
  pipeod_in numeric,
  pipeod_mm numeric,
  pipeweight double precision,
  pipegrade numeric,
  loadtally numeric,
  dateweilded date,
  datereceived date,
  dataenteredby character varying(50),
  deliveryno character varying(50),
  manufacturer character varying(50),
  Remarks character varying(100),
  ManualUser character varying(100),
  log_when timestamp,
  CONSTRAINT fieldtally1_pkey PRIMARY KEY (fieldtally1_id, pipeno)
  );
  Create rule fieldtally1_ins as on INSERT to fieldtally1
  Do Instead
  Insert into fieldtally1 values (
                New.pipeno,
                New.wthick,
                New.heatno1,
                New.pipeno2,
                New.heatno2,
                New.Djointno,
                New.ContractorNo,
                New.measuredlength,
                New.serialno,
                New.coatingtype,
                New.coatingno,
                New.mnfcno,
                New.factorylength,
                New.pipeod_in,
                New.pipeod_mm,
                New.pipeweight,
                New.pipegrade,
                New.ManualUser, 
                current_timestamp
                );

CREATE RULE fieldtally1_upd AS ON UPDATE TO fieldtally1
    DO INSTEAD
    UPDATE fieldtally1
    SET PIPENO = New.pipeno,
    wthick = New.wthick,
    heatno1 = New.heatno1,
    pipeno2 = New.pipeno2,
    heatno2 = New.heatno2,
    Djointno = New.Djointno,
    ContractorNo = New.ContractorNo,
    measuredlength = New.measuredlength,
    New.serialno = New.serialno,
    coatingtype = New.coatingtype,
    coatingno = New.coatingno,
    mnfcno = New.mnfcno,
    factorylength = New.factorylength,
    pipeod_in = New.pipeod_in,
    pipeod_mm = New.pipeod_mm,
    pipeweight = New.pipeweight,
    pipegrade =  New.pipegrade,
    ManualUser = New.ManualUser 
    WHERE pipeno = OLD.pipeno;      

CREATE RULE fieldtally1_del AS ON DELETE TO fieldtally1
    DO INSTEAD
    DELETE FROM fieldtally1
     WHERE pipeno = OLD.pipeno;
  

这是我运行脚本时得到的错误

     
    

注意:CREATE TABLE将为串行列创建隐式序列“fieldtally1_fieldtally1_id_seq”     “fieldtally1.fieldtally1_id”注意:CREATE TABLE / PRIMARY KEY将会     为表“fieldtally1”创建隐式索引“fieldtally1_pkey”     错误:列“fieldtally1_id”的类型为整数但表达式为     类型字符变化LINE 34:New.pipeno,                  ^提示:您需要重写或转换表达式。

  
     

*** 错误 ** *

     

错误:列“fieldtally1_id”的类型为整数但表达式为   类型字符变化SQL状态:42804提示:您需要重写   或者施放表达。字符:1024

2 个答案:

答案 0 :(得分:1)

如果您的插入与列完全不匹配,则需要指定它们。您的更新包含“新”。在serialno的左侧:这适用于http://sqlfiddle.com/#!1/c9ef3/1/0

CREATE TABLE fieldtally
(fieldtally_id serial NOT NULL primary key,
  pipeno character varying,
  wthick real,
  heatno1 character varying(32),
  pipeno2 character varying(32),
  heatno2 character varying(32),
  Djointno character varying(32),
  ContractorNo character varying(32),
  measuredlength double precision,
  serialno character varying(32),
  CoatingType character varying(32),
  coatingno character varying(32),
  mnfcno character varying(32),
  FactoryLength double precision,
  pipeod_in numeric,
  pipeod_mm numeric,
  pipeweight double precision,
  pipegrade numeric,
  loadtally numeric,
  dateweilded date,
  datereceived date,
  dataenteredby character varying(50),
  deliveryno character varying(50),
  manufacturer character varying(50),
  Remarks character varying(100),
  ManualUser text,
  log_when timestamp
  );
  Create rule fieldtally_ins as on INSERT to fieldtally
  Do Instead
  Insert into fieldtally (pipeno, wthick, heatno1, pipeno2, heatno2, djointno, contractorno, measuredlength, serialno, coatingtype, coatingno,
                         mnfcno, factorylength, pipeod_in, pipeod_mm, pipeweight, pipegrade, manualuser, log_when) values (
                New.pipeno,
                New.wthick,
                New.heatno1,
                New.pipeno2,
                New.heatno2,
                New.Djointno,
                New.ContractorNo,
                New.measuredlength,
                New.serialno,
                New.coatingtype,
                New.coatingno,
                New.mnfcno,
                New.factorylength,
                New.pipeod_in,
                New.pipeod_mm,
                New.pipeweight,
                New.pipegrade,
                New.ManualUser, 
                current_timestamp
                );

CREATE RULE fieldtally_upd AS ON UPDATE TO fieldtally
    DO INSTEAD
    UPDATE fieldtally
    SET PIPENO = New.pipeno,
    wthick = New.wthick,
    heatno1 = New.heatno1,
    pipeno2 = New.pipeno2,
    heatno2 = New.heatno2,
    Djointno = New.Djointno,
    ContractorNo = New.ContractorNo,
    measuredlength = New.measuredlength,
    serialno = New.serialno,
    coatingtype = New.coatingtype,
    coatingno = New.coatingno,
    mnfcno = New.mnfcno,
    factorylength = New.factorylength,
    pipeod_in = New.pipeod_in,
    pipeod_mm = New.pipeod_mm,
    pipeweight = New.pipeweight,
    pipegrade =  New.pipegrade,
    ManualUser = New.ManualUser 
    WHERE pipeno = OLD.pipeno;      

CREATE RULE fieldtally_del AS ON DELETE TO fieldtally
    DO INSTEAD
    DELETE FROM fieldtally
     WHERE pipeno = OLD.pipeno;

答案 1 :(得分:-2)

用户GO - 所以sql不会在最后一次检查之前检查验证

CREATE TABLE fieldtally
    (fieldtally_id serial NOT NULL primary key,
      pipeno character varying,
      wthick real,
      heatno1 character varying(32),
      pipeno2 character varying(32),
      heatno2 character varying(32),
      Djointno character varying(32),
      ContractorNo character varying(32),
      measuredlength double precision,
      serialno character varying(32),
      CoatingType character varying(32),
      coatingno character varying(32),
      mnfcno character varying(32),
      FactoryLength double precision,
      pipeod_in numeric,
      pipeod_mm numeric,
      pipeweight double precision,
      pipegrade numeric,
      loadtally numeric,
      dateweilded date,
      datereceived date,
      dataenteredby character varying(50),
      deliveryno character varying(50),
      manufacturer character varying(50),
      Remarks character varying(100),
      ManualUser text,
      log_when timestamp
      );
GO
      Create rule fieldtally_ins as on INSERT to fieldtally
      Do Instead
      Insert into fieldtally values (
                    New.pipeno,
                    New.wthick,
                    New.heatno1,
                    New.pipeno2,
                    New.heatno2,
                    New.Djointno,
                    New.ContractorNo,
                    New.measuredlength,
                    New.serialno,
                    New.coatingtype,
                    New.coatingno,
                    New.mnfcno,
                    New.factorylength,
                    New.pipeod_in,
                    New.pipeod_mm,
                    New.pipeweight,
                    New.pipegrade,
                    New.ManualUser, 
                    current_timestamp
                    );
GO
        CREATE RULE fieldtally_upd AS ON UPDATE TO fieldtally
            DO INSTEAD
            UPDATE fieldtally
            SET PIPENO = New.pipeno,
            wthick = New.wthick,
            heatno1 = New.heatno1,
            pipeno2 = New.pipeno2,
            heatno2 = New.heatno2,
            Djointno = New.Djointno,
            ContractorNo = New.ContractorNo,
            measuredlength = New.measuredlength,
            New.serialno = New.serialno,
            coatingtype = New.coatingtype,
            coatingno = New.coatingno,
            mnfcno = New.mnfcno,
            factorylength = New.factorylength,
            pipeod_in = New.pipeod_in,
            pipeod_mm = New.pipeod_mm,
            pipeweight = New.pipeweight,
            pipegrade =  New.pipegrade,
            ManualUser = New.ManualUser 
            WHERE pipeno = OLD.pipeno;      
    GO
        CREATE RULE fieldtally_del AS ON DELETE TO fieldtally
            DO INSTEAD
            DELETE FROM fieldtally
             WHERE pipeno = OLD.pipeno;