无效的对象名称和无效的列名称

时间:2017-04-28 19:03:03

标签: sql-server triggers

我收到无效对象名称'CUSTOMER_temp'。错误代码。我制作了这两个表并制作了临时表。当我从插入状态时。它给我错误代码无效列名'state'。我不确定我是否还需要它,或者我是否能够摆脱它。

此触发器的目的是自动将新记录复制到新表中。

DROP TABLE CUSTOMER
CREATE TABLE CUSTOMER
(
CustomerID CHAR(5) PRIMARY KEY, --Make Primary Key
CustLastName VARCHAR(20),  
CustFirstName VARCHAR(20),
CustStreet VARCHAR(60),
CustCity VARCHAR(30),
CustState CHAR(2),
CustZip CHAR(5),
CustPhone CHAR(10),
CustEmail CHAR(50),
);


drop table CUSTOMER_temp

CREATE TABLE CUSTOMER_temp  -- temporary table
(
CustomerID CHAR(5) PRIMARY KEY, 
CustLastName VARCHAR(20),  
CustFirstName VARCHAR(20),
CustStreet VARCHAR(60),
CustCity VARCHAR(30),
CustState CHAR(2),
CustZip CHAR(5),
CustPhone CHAR(10),
CustEmail CHAR(50),
);




  CREATE TRIGGER dbo.CustCopy
 On CUSTOMER
 AFTER INSERT
  AS
  BEGIN
 SET NOCOUNT ON;
  insert  CUSTOMER_temp (CustomerID, CustLastName, CustPhone, CustState)
    Select CustomerID, CustLastName, CustPhone, CustState from inserted
    END

1 个答案:

答案 0 :(得分:0)

试试这个...... INTO后需要Insert

更新已移除dbo.

CREATE TRIGGER dbo.CustCopy
    On CUSTOMER
    AFTER INSERT
    AS
    BEGIN
    SET NOCOUNT ON;
    insert INTO CUSTOMER_temp (CustomerID, CustLastName, CustPhone, CustState)
    Select CustomerID, CustLastName, CustPhone, CustState from inserted
    END