从两个逗号分隔的字符串参数更新 SQL Server 中的表 [英] Update table in SQL Server from two comma separated string parameter
问题描述
可能的重复:
从两个逗号分隔的参数作为输入更新表格一个>
我在前端有一个 Gridview,其中 Grid 有两列:ID 和 Order,如下所示:
I have a Gridview in front end where Grid have two columns : ID and Order like this:
ID Order
1 2
2 4
3 1
4 3
订单栏是可编辑的.现在,如果我想更新订单并进行保存,我想将其存储到数据库中.我将 ID 和 Order 存储为逗号分隔的字符串,如 sID(1,2,3,4) 和 sOrder(2,4,1,3) 并作为输入参数发送到 SQL Server.通过存储过程如何更新到表中.
Order column is editable. Now if I want to update the order and make save I want to store it into database. I have stored ID and Order as a comma separated string like sID(1,2,3,4) and sOrder(2,4,1,3) and sent to SQL Server as input parameters. Through Stored procedure how can update into the table.
推荐答案
来自各种网站的来源:
DECLARE @sID nvarchar(max) = '1,2,3,4'
DECLARE @sOrder nvarchar(max) = '2,4,1,3'
DECLARE @Split char(1) = ','
DECLARE @xSID xml
DECLARE @xOrder xml
SELECT @xSID = CONVERT(xml,'<root><s>' + REPLACE(@sID, @Split,'</s><s>') + '</s></root>')
SELECT @xOrder = CONVERT(xml,'<root><s>' + REPLACE(@sOrder, @Split,'</s><s>') + '</s></root>')
SELECT [Value] = T.c.value('.','varchar(20)') FROM @xSID.nodes('/root/s') T(c)
SELECT [Value] = T.c.value('.','varchar(20)') FROM @xOrder.nodes('/root/s') T(c)
将 @sID
和 @sOrder
替换为 SP 的参数.
replace the @sID
and @sOrder
as parameters for you SP.
必须补充一点,这个网站几乎回答了这个问题:社交 MSDN
Must add that this site pretty much answered the question: Social MSDN
这篇关于从两个逗号分隔的字符串参数更新 SQL Server 中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!