Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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.
Posted
Updated 3-Feb-13 20:21pm
v2
Comments
madhuri@mumbai 4-Feb-13 0:58am    
you mean to say that,u have sended comma separated string to SP i.e '2,3,4,5' right
so you just take input param ID as varchar in SP and then update it.
like update temp set ID='2,3,4,5'

Separate ids and order using tokenize function(use below function).
This function return table variable. Using cursor, fetch rows from table variable and update main table.

SQL
CREATE FUNCTION [dbo].[String_Tokenizer]
(
	@RowData nvarchar(max),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	Data nvarchar(100)
) 
AS  
BEGIN 
	Declare @Cnt int
	Set @Cnt = 1
 
	While (Charindex(@SplitOn,@RowData)>0)
	Begin
		Insert Into @RtnValue (data)
		Select 
			Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
 
		Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
		Set @Cnt = @Cnt + 1
	End
	
	Insert Into @RtnValue (data)
	Select Data = ltrim(rtrim(@RowData))
 
	Return
END
 
Share this answer
 
create this function
SQL
CREATE Function [dbo].[fn_Splitter] (@IDs Varchar(100))  
Returns @Tbl_IDs Table (ID Int)  As  
Begin 
 -- Append comma
 Set @IDs =  @IDs + ',' 
 -- Indexes to keep the position of searching
 Declare @Pos1 Int
 Declare @pos2 Int
  -- Start from first character 
 Set @Pos1=1
 Set @Pos2=1
 While @Pos1<len(@ids)>
 Begin
  Set @Pos1 = CharIndex(',',@IDs,@Pos1)
  Insert @Tbl_IDs Select  Cast(Substring(@IDs,@Pos2,@Pos1-@Pos2) As Int)
  -- Go to next non comma character
  Set @Pos2=@Pos1+1
  -- Search from the next charcater
  Set @Pos1 = @Pos1+1
 End 
 Return  
 End


now try this query
SQL
with tbl_Ids
(
    select row_number() over(partition by Id) as srno,Id 
    from dbo.fn_splitter('1,2,3,4') as tbl_Id
)

select tbl_Ids.Id,tbl_Orders.Order from tbl_Ids
left join
(
    select row_number() over(partition by Id) as srno,Id as Order 
    from dbo.fn_splitter('2,4,1,3') as tbl_Order
) as tbl_Orders on tbl_Ids.srno=tbl_Order.srno

Happy Coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900