日期:2014-05-18  浏览次数:20605 次

这样怎么写
当表processpdproduct 字段prodnum内容为04,05,07,08,09,011,012,013,014,016,10,11,19时字段master不能为NULL,数据不让插入.

------解决方案--------------------
SQL code

if object_id('tb','U') is not null
   drop table tb
go
create table tb
(
 prodnum varchar(10),
 master int
)
go
if object_id('tr_tb','TR') is not null
   drop trigger tr_tb
go
create trigger tr_tb on tb
for insert
as
  if exists(select 1 from inserted where prodnum in('04','05','07','08','09','011','012','013','014','016','10','11','19')) and exists(select 1 from inserted where master is null)
  begin
    rollback
    print('prodnum 不允许为 04,05,07,08,09,011,012,013,014,016,10,11,19 其中一个')
  end
go
insert into tb values ('04',2)  --允许插入
insert into tb values ('06',null) --允许插入
insert into tb values ('08',3)  --允许插入
insert into tb values ('09',null) --不允许插入
/*
prodnum    master
---------- -----------
04         2
06         NULL
08         3

(3 行受影响)
*/

------解决方案--------------------
探讨
前台判断比较好。能不用触发器就不用触发器。