数据批量插入
页面提供输入框,供用户输入用逗号分隔的一组数据。
程序中用作一定的Check提示分隔的值不可以为空。
然后传入数据库。
存储过程实现:
declare @InsertNos nvarchar(max) -- 存储过程参数,拼好的字符串
declare @InsertNo nvarchar(20) -- 这个是我们要插入的字段
set @InsertNos = @InsertNos +',' -- 在末尾加上 ',' , 防止出现死循环, 要保证这个字符串后面有且仅有一个 ',', 不然插入的数据会产生空值。
SET NOCOUNT ON;
WHILE (@InsertNos is not null and @InsertNos <> '')
BEGIN
set @InsertNo = substring(@InsertNos ,1,charindex(',',@InsertNos ,1)-1) -- 截取第一个逗号前面的值
insert into xxxxxxx @InsertNo -- 将这个值插入DB
-- 截取 第一个逗号之后的值,再次循环直到为 ''
set @InsertNos = substring(@InsertNos ,charindex(',',@InsertNos ,1)+1, len(@InsertNos ) - charindex(',',@InsertNos ,1))
END