Update a table from two comma separated parameter as input

后端 未结 3 478
醉话见心
醉话见心 2021-01-23 01:02

I have a Gridview in front end where Grid have two columns : ID and Order like this:

 ID        Order

 1           1
 2           2
 3           3
 4                    


        
3条回答
  •  深忆病人
    2021-01-23 01:34

    You can use charindex like

    DECLARE @id VARCHAR(MAX)
    DECLARE @order VARCHAR(MAX)
    
    SET @id='1,2,3,4,'
    SET @order='2,4,1,3,'
    
    WHILE CHARINDEX(',',@id) > 0
    BEGIN
    DECLARE @tmpid VARCHAR(50)
    SET @tmpid=SUBSTRING(@id,1,(charindex(',',@id)-1))
    DECLARE @tmporder VARCHAR(50)
    SET @tmporder=SUBSTRING(@order,1,(charindex(',',@order)-1))
    
    UPDATE dbo.Test SET
    [Order]=@tmporder
    WHERE ID=convert(int,@tmpid)
    
    SET @id = SUBSTRING(@id,charindex(',',@id)+1,len(@id))
    SET @order=SUBSTRING(@order,charindex(',',@order)+1,len(@order))
    END
    

提交回复
热议问题