前端提交一串逗号分割的字符串,经存储过程,存入sql数据表中。
表如下:
create table [dbo].[miscellaneous] ( [id] int identity(1,1) not null, [item] nvarchar(50) not null default(n'') ) go
准备存储过程:
create procedure [dbo].[usp_miscellaneous_insert] ( @comma_delimited_string nvarchar(max) ) as begin declare @xml xml = [dbo].[svf_converttoxml](@comma_delimited_string) insert into [dbo].[miscellaneous](item) select nref.value('.','nvarchar(max)') as [item] from @xml.nodes('/insus') as r(nref) end go
上面#43行代码,有一个自定义函数,它是把逗号分割的字符串转为xml格式。
参考《符号分割的字符串转换为xml》
举个例子:
declare @str nvarchar(max) = n'金,水,木,火,土' execute [dbo].[usp_miscellaneous_insert] @str select [id],[item] from [dbo].[miscellaneous]