Home Forums SQL Server 7,2000 T-SQL Parmeterized list to a stored procedure RE: Parmeterized list to a stored procedure

  • It could be that what reddyk needs is the ability to receive a comma-delimited string of arguments and parse out the individual arguments, or something similar.  If so, the following code might be enough to get you started (pretend that the whole snippet of code is really a procedure definition, and that @myString is really an input parameter).  I did this in a hurry, without referring to any existing code templates, so make sure I'm not suffering from off-by-one-itis or any other similar problems before adopting this approach.  The important thing is that you can use charindex:

    declare @myString varchar(80) -- paramater to be parsed

    set @myString = 'arg1,arg2,arg3,arg4'

    declare @delimiter char(1) -- delimiter within @myString

    set @delimiter = ','

    declare @start int -- starting point of current arg in @myString

    set @start = 0

    declare @currentDelimiter int -- position of current comma (or end-of-string) in @myString

    set @currentDelimiter = 0

    declare @arg varchar(80) -- current arg

    while @currentDelimiter <= len(@myString)

    begin

       set @start = @currentDelimiter + 1

       set @currentDelimiter= charindex (@delimiter, @myString, @start)

       if @currentDelimiter = 0

          set @currentDelimiter = len(@myString) + 1

       set @arg = substring (@myString, @start, @currentDelimiter - @start)

       print @arg

    end

    Good luck!

    Chris