Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2003
    Posts
    3

    Unanswered: passing a comma delimited string to stored procedure

    Hello,

    I have an asp page that sends a string,
    ex. CO,S2,S3,S4,S5,S6,SA,SB,SD,SF,SG,SO,SQ,SR,ST
    to a stored procedure in sql server as a single variable
    (example @str).

    I want to then somehow split the variable's contents up as 'CO', 'S2', etc to use in a select statement's WHERE IN clause.

    Ive tried the replace function to replace the , (comma) with ',' but didnt get the right syntax possibly....

    Anybody have any leads or samples done before for this.

    Very much appreciated in advance.

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    What about this idea?

    drop proc test2
    go
    create proc test2 @line varchar(8000)
    as
    declare @sql varchar(8000)
    select @sql='select ''ok'' where ''A'' in('+@line+')'
    select @sql
    exec(@sql)
    go
    test2 '''A'',''B'',''C'',''D'''
    go

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    I assume you are going to use this in a dynamic SQL statement?

    You say you got a syntax error. Did you remember to put single quotes before and after the string (as snail illustrates), as well as around the commas?

    I find it helpful in debugging dynamic code to construct the code in a variable and then PRINT the variable immediately before executing it. If you do this and you are still having problems, post your SQL so that we can review it.

    blindman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •