Hi, I am trying to do a loop while a list of array is assigned ('CHP,CNH,COW') ... I am using comma seperator to get each list value ... but, it donest really do what I am trying to do ... pls help!!! How do I loop through each value and do the rest ...??
=====================================
DECLARE @.ABBR AS NVARCHAR(50)
SET @.ABBR = 'CHP,CNH,COW'
DECLARE @.SEP AS NVARCHAR(5)
SET @.SEP = ','
WHILE patindex('%,' + @.ABBR + ',%', @.ABBR ) > 0
BEGIN
-- do the rest
END
I used a stored procedure as thisCreate proceduresp_ParseArray( @.Arrayvarchar(1000),@.separatorchar(1) )ASset nocount on-- @.Array is the array we wish to parse-- @.Separator is the separator charactor such as a commadeclare @.separator_positionint-- This is used to locate each separator characterdeclare @.array_valuevarchar(1000)-- this holds each array value as it is returned-- For my loop to work I need an extra separator at the end. I always look to the-- left of the separator character for each array valueset @.array = @.array + @.separator-- Loop through the string searching for separtor characterswhile patindex('%' + @.separator +'%' , @.array) <> 0begin-- patindex matches the a pattern against a stringselect @.separator_position = patindex('%' + @.separator +'%' , @.array)select @.array_value =left(@.array, @.separator_position - 1)-- This is where you process the values passed. -- Replace this select statement with your processing -- @.array_value holds the value of this element of the arrayselect Array_Value = @.array_value-- This replaces what we just processed with and empty stringselect @.array = stuff(@.array, 1, @.separator_position,'')endset nocount offgoHope this helpsql
No comments:
Post a Comment