当前位置: 代码迷 >> Sql Server >> 这样如何写
  详细解决方案

这样如何写

热度:15   发布时间:2016-04-27 14:49:46.0
这样怎么写
当表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 tbgocreate table tb( prodnum varchar(10), master int)goif object_id('tr_tb','TR') is not null   drop trigger tr_tbgocreate trigger tr_tb on tbfor insertas  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 其中一个')  endgoinsert 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         206         NULL08         3(3 行受影响)*/
------解决方案--------------------
探讨
前台判断比较好。能不用触发器就不用触发器。
  相关解决方案