本文介绍了如何在storedprocedure中拆分字符串后插入一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我的存储过程 创建 procedure sptest( @ roleid int , @ privileges varchar ( 50 )) as begin insert 进入用户(Roleid,角色)值(roleid = @ roleid,privileges = @ privileges) end @privileges包含 R,W,D 我们需要将它存储在数据库中,如下所示 Roleid Prv 1 R 1 W 1 D 我们怎么能这样做......? 我可以使用循环或其他东西.. 解决方案 打破它并不是那么简单,但在SQL IN子句中使用逗号分隔值参数字符串 [ ^ ]使用临时表执行您想要的操作。 是的,您可以在SQL中拆分列表,然后处理插入每个项目的循环。 这里有类似的事情: 在存储过程中解析分隔的字符串 [ ^ ] SQL中没有内置函数来分割逗号分隔值。你必须创建用户定义的函数来分割逗号分隔的数据.. 请参考以下链接,其中包含用于分割数据的用户定义函数的详细信息.. SQL中的分割功能 [ ^ ] 创建用户定义函数以在SQL中拆分数据后,您可以在表中插入值,如下所示 CREATE PROCEDURE procTest( @roleID INT , @ privileges VARCHAR ( 50 )) AS 乙EGIN INSERT INTO tblRoles(RoleID,Roles) SELECT @ roleID ,值 FROM dbo。[名称 - 用户定义的分割功能]( @ privileges ,' ,') END This is my stored procedurecreate procedure sptest(@roleid int, @privileges varchar(50)) as begin insert into users(Roleid, Roles) values(roleid=@roleid , privileges=@privileges) end@privileges contains R,W,DWe need to store it in Database like belowRoleid Prv1 R1 W1 DHow can we do that...?Can i use Loop or something else.. 解决方案 Breaking it isn't that simple, but Using comma separated value parameter strings in SQL IN clauses[^] does what you want with a temporary table.Yes you could split the list in SQL and then work on a loop inserting each item.There is a similar thing being done here: Parse delimited string in a Stored procedure[^]There is no built in function in SQL to split the comma separaed values. You have to create user defined function which split the comma separated data..Please refer following link containing the details of User Defined function used to split the data..Split function in SQL[^]Once you created the user defined function to split data in SQL, you can insert values into your table as followingCREATE PROCEDURE procTest(@roleID INT, @privileges VARCHAR(50)) AS BEGIN INSERT INTO tblRoles(RoleID, Roles) SELECT @roleID, value FROM dbo.[NAME-OF-USER-DEFINED-SPLIT-FUNCTION](@privileges,',') END 这篇关于如何在storedprocedure中拆分字符串后插入一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 15:00