读书人

insert操作能否仅仅让符合约束的记录

发布时间: 2012-01-03 22:16:06 作者: rapoo

insert操作,能否仅仅让符合约束的记录被插入?
insert into 表1(片区, 编号, 姓名) SELECT 片区, 编号, 姓名 from OPENROWSET( 'MICROSOFT.JET.OLEDB.4.0 ', 'Excel .0;HDR=YES;DATABASE=f:\111.xls ',data)
以上是插入记录的语句,可是那个excel表中的记录有时候有空行(主要是表尾的空行很难识别),有什么好办法避免从外部excel表中插入空记录呢。
假设我把姓名字段设为“不为空”,那么一旦遇到空记录被插入将导致这个insert操作回滚。有什么办法避免呢?如何让有效记录被插入而无效记录被拒之门外?

[解决办法]
这样试试

create trigger itr_表1 on 表1
instead of insert
as
insert into 表1(片区,编号,姓名)
SELECT 片区,编号,姓名
from inserted
where 片区 is no null and 编号 is not null and 姓名 is not null
[解决办法]
加 as a where not(a.片区 is null or a.编号 or isnull and.姓名 or is null)
[解决办法]
用字段列用null就行了
col1 is null
data--这里需要加data$符号

读书人网 >SQL Server

热点推荐