Update table in SQL Server from two comma separated string parameter [duplicate]

▼魔方 西西 提交于 2019-12-13 08:18:46

问题


Possible Duplicate:
Update a table from two comma separated parameter as input

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

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.


回答1:


you can create a Function that your Stored Proc can call to split the input parameters.

Refer to: How do I split a string so I can access item x?




回答2:


Sources from various sites to come up with this:

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)

replace the @sID and @sOrder as parameters for you SP.

Must add that this site pretty much answered the question: Social MSDN



来源:https://stackoverflow.com/questions/14681084/update-table-in-sql-server-from-two-comma-separated-string-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!