Friday, January 9, 2009

Template for SP geting values one by one from CSV (Comma Seprated Values)

HI,
Many time we need to update many records in database. for example if we using select all check boxes for delete or update the reocrds then we can save the selected id corespondit to selected check boxes and then pass all vlues in dataset as the form of CSV and the with the help of following SP we can get values one by one and
fire the query.
Template for SP to get values one by one from CSV. suppy two inputs first the csv and then charter which i used for seprate the values



create proc [ufn_CountChar] ( @pInput VARCHAR(1000), @pSearchChar CHAR(1) )
as
BEGIN

/*DECLARE @vInputLength INT*/
DECLARE @vIndex INT
DECLARE @vCount INT
DECLARE @s varchar(2)
DECLARE @str varchar(10)
declare @SubStr varchar(1000)

set @SubStr=''
SET @vCount = 0
SET @vIndex = 1
/*SET @vInputLength = LEN(@pInput)*/
set @str=''

WHILE @vIndex <= len(@pInput)
BEGIN
IF SUBSTRING(@pInput, @vIndex, 1) = @pSearchChar
BEGIN
SET @vCount = @vCount + 1
set @SubStr = substring(@pInput,len(@SubStr),len( @str))
select 'update insert delete query with this value - ' + @str
set @str=''
END
else
BEGIN
set @s=SUBSTRING(@pInput,@vindex,1)
set @str=@str+@s
print ' i m in else part'+ space(3)+ @str
if( @vIndex = len(@pInput))
begin
select 'update insert delete query with this value - ' + @str
end

END
SET @vIndex = @vIndex + 1

END

--select @str


END
GO

--ufn_CountChar 'afd,hghg,hgjgh',','

No comments: