SQL Server:在“当前”表更新到“历史”表时创建触发器

时间:2016-01-29 19:42:38

标签: sql-server oracle triggers

我正在努力从Oracle 9i数据库迁移到SQL Server并尝试解决并转换我们必须保留记录历史记录的一个触发器。 static void Main(string[] args) { int start = 0; int end = 100; Random myRandom = new Random(); int computer = myRandom.Next(start, end); int count = 0; int guess; while (true) { guess = (end - start) / 2 + start; count = count + 1; Console.WriteLine("Count {0}", count); if (guess > computer) { end = guess; Console.WriteLine("Your guess is too high, next guess: {0}", guess); } else if (guess < computer) { start = guess; Console.WriteLine("Your guess is too low, guess again: {0}", guess); } else { break; } } Console.WriteLine("You got it. The number is {0}. It took you {1} guesses.", computer, count); Console.ReadKey(); } 表具有自己的History列,所有其他列与ID表中的列相同。

这是来自OraDb的我的触发器:(在OraDb中我们使用序列来增加“历史”PK)

Current

我尝试过以下操作,但收到错误消息

  

INSERT语句的选择列表包含的项目少于
  插入列表。 SELECT值的数量必须与INSERT的数量相匹配   列。

代码:

CREATE OR REPLACE TRIGGER "RC_CURRENT_T" 
BEFORE INSERT OR UPDATE
ON RC_CURRENT 
FOR EACH ROW 

  DECLARE var_date VARCHAR2(30 BYTE);
  BEGIN
    IF UPDATING THEN
      var_date:=TO_CHAR(CURRENT_DATE, 'MM/DD/YYYY HH24:MI:SS');
      INSERT
      INTO RC_HISTORY
        (
          "ID" ,
          REC_ID ,
          STATUS_DESCR ,
          REQBY_FULLNAME ,
          REQBY_USERNAME ,
          REQBY_EMAIL ,
          REQBY_EMPLID ,
          PCARD_TYPE ,
          BUS_SGMT_DESCR ,
          REQ_TYPE ,
          REQ_CHNGAUTHBY ,
          OP_CNTR_NAME ,
          CARDHOLDER_NAME ,
          CUSTODIAN_NAME ,
          ED_ROLLUP_NUM ,
          CARDHOLDER_ADDRESS ,
          CARDHOLDER_CITY ,
          CARDHOLDER_STATE ,
          CARDHOLDER_ZIP ,
          CARD_NUMBER ,
          GL_CODE ,
          GL_LOCATION ,
          GL_DEPARTMENT ,
          GL_ACCOUNT ,
          GL_SVCSCODE ,
          MONTH_LIMIT ,
          LIMIT_RESTRICTIONS ,
          CARD_LIMIT ,
          REASON_COMMENTS ,
          CREATE_DATE ,
          APPROVER1_NAME ,
          APPROVER1_USERNAME ,
          APPROVER1_EMAIL ,
          APPROVER1_EMPLID ,
          APPROVER1_DATE ,
          APPROVER2_NAME ,
          APPROVER2_USERNAME ,
          APPROVER2_EMAIL ,
          APPROVER2_EMPLID ,
          APPROVER2_DATE ,
          ADMIN_LVL1 ,
          ADMIN_LVL2 ,
          ADMIN_LVL3 ,
          ADMIN_LVL4 ,
          ADMIN_LVL5 ,
          ADMIN_LVL6 ,
          KEYED_DATE ,
          KEYED_BY_NAME ,
          KEYER_CHANGE_TYPE ,
          KEYER_COMMENTS ,
          CARD_STATUS ,
          KEYER_USERNAME ,
          KEYER_EMPLID ,
          USER_FULLNAME,
          OPER_STREET,
          OPER_CITY,
          OPER_STATE,
          OPER_ZIP,
          USER_PHONE,
          USER_EMAIL,
          USER_HYCHY_SETUP,
          USER_ASSIGNED_USERID,
          USER_PROCESS_DATE,
          USER_DISTRIBUTION,
          USER_REQUESTTYPE,
          REC_ENTRY_DATE
        )
        VALUES
        (
          RC_HISTORY_SEQ.nextval ,
          :OLD.REC_ID ,
          :OLD.STATUS_DESCR ,
          :OLD.REQBY_FULLNAME ,
          :OLD.REQBY_USERNAME ,
          :OLD.REQBY_EMAIL ,
          :OLD.REQBY_EMPLID ,
          :OLD.PCARD_TYPE ,
          :OLD.BUS_SGMT_DESCR ,
          :OLD.REQ_TYPE ,
          :OLD.REQ_CHNGAUTHBY ,
          :OLD.OP_CNTR_NAME ,
          :OLD.CARDHOLDER_NAME ,
          :OLD.CUSTODIAN_NAME ,
          :OLD.ED_ROLLUP_NUM ,
          :OLD.CARDHOLDER_ADDRESS ,
          :OLD.CARDHOLDER_CITY ,
          :OLD.CARDHOLDER_STATE ,
          :OLD.CARDHOLDER_ZIP ,
          :OLD.CARD_NUMBER ,
          :OLD.GL_CODE ,
          :OLD.GL_LOCATION ,
          :OLD.GL_DEPARTMENT ,
          :OLD.GL_ACCOUNT ,
          :OLD.GL_SVCSCODE ,
          :OLD.MONTH_LIMIT ,
          :OLD.LIMIT_RESTRICTIONS ,
          :OLD.CARD_LIMIT ,
          :OLD.REASON_COMMENTS ,
          :OLD.CREATE_DATE ,
          :OLD.APPROVER1_NAME ,
          :OLD.APPROVER1_USERNAME ,
          :OLD.APPROVER1_EMAIL ,
          :OLD.APPROVER1_EMPLID ,
          :OLD.APPROVER1_DATE ,
          :OLD.APPROVER2_NAME ,
          :OLD.APPROVER2_USERNAME ,
          :OLD.APPROVER2_EMAIL ,
          :OLD.APPROVER2_EMPLID ,
          :OLD.APPROVER2_DATE ,
          :OLD.ADMIN_LVL1 ,
          :OLD.ADMIN_LVL2 ,
          :OLD.ADMIN_LVL3 ,
          :OLD.ADMIN_LVL4 ,
          :OLD.ADMIN_LVL5 ,
          :OLD.ADMIN_LVL6 ,
          :OLD.KEYED_DATE ,
          :OLD.KEYED_BY_NAME ,
          :OLD.KEYER_CHANGE_TYPE ,
          :OLD.KEYER_COMMENTS ,
          :OLD.CARD_STATUS ,
          :OLD.KEYER_USERNAME ,
          :OLD.KEYER_EMPLID ,
          :OLD.USER_FULLNAME,
          :OLD.OPER_STREET,
          :OLD.OPER_CITY,
          :OLD.OPER_STATE,
          :OLD.OPER_ZIP,
          :OLD.USER_PHONE,
          :OLD.USER_EMAIL,
          :OLD.USER_HYCHY_SETUP,
          :OLD.USER_ASSIGNED_USERID,
          :OLD.USER_PROCESS_DATE,
          :OLD.USER_DISTRIBUTION,
          :OLD.USER_REQUESTTYPE,
          var_date
        ) ;
    END IF;
  END;

我需要在历史记录表中维护单独的ID以用于报告目的。

非常感谢任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

查看旧触发器(和错误消息)RC_HISTORYRC_CURRENT相比有1个额外字段,因此您有两个选项。

  1. 如果[REC_ID]是具有自动增量的标识列,您只需将其从要插入的列列表中删除即可

    INSERT INTO [dbo].[RC_HISTORY]
    ([STATUS_DESCR]
    ,[REQBY_FULLNAME]
    ,[REQBY_USERNAME]
    ...
    
  2. 如果[REC_ID]未设置为自动递增,则必须提供一个值以插入列中

    SELECT (SELECT MAX([REC_ID])+1 FROM RC_HISTORY), * FROM RC_CURRENT
    
  3. 我建议您尝试第一个选项并将列设置为标识列(如果尚未添加),我还建议您还使用命名列而不是*作为选择值。