日期:2014-05-18 浏览次数:20599 次
捕获错误还是在程序中好,TRY CATCH, A. 创建特定消息 下例显示可能出现的两种错误。第一种错误很简单,生成的是静态消息。第二种错误则是在尝试修改的基础上动态生成的。 CREATE TRIGGER employee_insupd ON employee FOR INSERT, UPDATE AS /* Get the range of level for this job type from the jobs table. */ DECLARE @@MIN_LVL tinyint, @@MAX_LVL tinyint, @@EMP_LVL tinyint, @@JOB_ID smallint SELECT @@MIN_LVl = min_lvl, @@MAX_LV = max_lvl, @@ EMP_LVL = i.job_lvl, @@JOB_ID = i.job_id FROM employee e, jobs j, inserted i WHERE e.emp_id = i.emp_id AND i.job_id = j.job_id IF (@@JOB_ID = 1) and (@@EMP_lVl <> 10) BEGIN RAISERROR ('Job id 1 expects the default level of 10.', 16, 1) ROLLBACK TRANSACTION END ELSE IF NOT @@ EMP_LVL BETWEEN @@MIN_LVL AND @@MAX_LVL) BEGIN RAISERROR ('The level for job_id:%d should be between %d and %d.', 16, 1, @@JOB_ID, @@MIN_LVL, @@MAX_LVL) ROLLBACK TRANSACTION END SQL中