通过一个参数将复选框列表多个选定值列表到存储过程

时间:2014-01-30 10:32:03

标签: sql-server-2008 stored-procedures checkboxlist multivalue multipleselection

我有复选框列表来选择多个值,现在我想使用一个参数将这个多个选定值传递给我的存储过程

insert into tblAccessRights (roleid,customerid,SubcustomerId)
values(@_roleid,@CustomerId,@SubcustomerId,)

在上面的查询中@SubcustomerId是一个参数,它将保存checkboxlist中的多个选定值 所以,现在条目应如下所示

roleid customerid subcustomerid
 1        2           6
 1        2           7
 1        2           8

此subustomeromerid是复选框中的多个选中值,如何通过一个参数实现此目的,如果有任何其他方式,请回复

如果我将checkboxlist值作为逗号分隔字符串并相应地将其传递给动态插入查询,是否可以使用动态查询完成...

1 个答案:

答案 0 :(得分:0)

我终于得到了解决方案,!!!!我给出了我的解决方案

我将字符串中的复选框列表作为逗号分隔值并将其传递给存储过程然后使用动态查询将此值插入如下:
   

 
create table tblrmp
 ( id int identity (1,1),
 name varchar(20), 
subcat varchar(20) ) 

declare @S varchar(20) set @S = '1,2,3,4,5' declare @sql varchar(max) while len(@S) > 0 begin print left(@S, charindex(',', @S+',')-1) set @sql='insert into tblrmp values(''abc'','+left(@S, charindex(',', @S+',')-1)+')' exec(@sql) set @S = stuff(@S, 1, charindex(',', @S+','), '') end

output: id name subcat 1 abc 1 2 abc 2 3 abc 3 4 abc 4 5 abc 5