日期:2014-05-17  浏览次数:20439 次

帮忙下这个sql语句为什么不能创建表
create table test
(
id int not null primary key identity(1,1),
xy int  check( xy>[min] and xy<[max]),
[min] int default(0),
[max] int default(1000)
)

------解决方案--------------------
引用:
这样是可以的,但是这样不是我的目的呀,我想让xy的取值介于min和max之间


这个得建立触发器:


create table test
(
id int not null primary key identity(1,1),
xy int ,
[min] int default(0),
[max] int default(1000)
)
go

create trigger dbo.trigger_test_insert
on test
for insert
as

if exists(select * from test where  not(xy>[min] and xy<[max]))
   rollback

go

--1.满足,不会报错
insert into test
select 10,1,11


--2.不满足条件,所以报错,插入不了
insert into test
select 10,1,10
/*
消息 3609,级别 16,状态 1,第 1 行
事务在触发器中结束。批处理已中止。
*/