逗号分割字符串经存储过程存入数据表中,字符串存储过程
前端提交一串逗号分割的字符串,经存储过程,存入SQL数据表中。
表如下:


CREATE TABLE [dbo].
[Miscellaneous]
(
[ID] INT IDENTITY(
1,
1)
NOT NULL,
[Item] NVARCHAR(
50)
NOT NULL DEFAULT(N
'')
)
GO
Source Code
准备存储过程:


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
Source Code
上面#43行代码,有一个自定义函数,它是把逗号分割的字符串转为XML格式。
参考《符号分割的字符串转换为XML》https://www.cnblogs.com/insus/p/10928041.html
举个例子:


DECLARE @str NVARCHAR(
MAX)
= N
'金,水,木,火,土'
EXECUTE [dbo].
[usp_Miscellaneous_Insert] @str
SELECT [ID],
[Item] FROM [dbo].
[Miscellaneous]
Source Code
http://www.htsjk.com/Sql_Server/24849.html
www.htsjk.Com
true
http://www.htsjk.com/Sql_Server/24849.html
NewsArticle
逗号分割字符串经存储过程存入数据表中,字符串存储过程 前端提交一串逗号分割的字符串,经存储过程,存入SQL数据表中。 表如下: CREATE TABLE [ dbo ] . [ Miscellaneous ] ( [ ID ] INT IDENTI...
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。