SQL Stored Procedures and Lists of Strings

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default SQL Stored Procedures and Lists of Strings

    Hey all. I've been pulling my hair out all day today trying to get this to
    work. I can do what i want to do in a straight <cfquery>, but i'm trying to do
    it in a SP for better performance etc.

    Basically, I've set up Contribute templates for my client for certain holiday
    destinations. My client can then insert a comma seperated list of city codes
    into the TemplateParameter which then gets passed to my CF code to retreive the
    latest specials.

    Using a straight query such as;

    SELECT * FROM WebSpecial
    WHERE ProductType = 'Package'
    AND CityCode IN (<cfqueryparam list="yes" value="#SQLCityCodeList#"
    cfsqltype="cf_sql_varchar" separator=",">)
    AND BrandID = 1
    ORDER BY ItemPriority

    Works fine when SQLCityCodeList = 'LON,PAR" for example. But when i try to use
    in a stored procedure, it just won't work.

    <cfstoredproc procedure = "DestinationSpecialsByCityCodes" dataSource="#DS#"
    username="#DSUN#" password="#DSPW#" debug="yes">
    <cfprocresult name = DestinationSpecials maxrows="100">
    <cfprocparam type="IN" CFSQLType=CF_SQL_INTEGER value="1" dbVarName=@BrandID>
    <cfprocparam type="IN" CFSQLType=CF_SQL_VARCHAR value="Package"
    dbVarName=@ProductType>
    <cfprocparam type="in" CFSQLType=CF_SQL_VARCHAR value="LON,PAR"
    dbVarName=@CityCodes>
    </cfstoredproc>

    I've hard coded the list just for testing purposes. The SP is as follows;

    CREATE PROCEDURE DestinationSpecialsByCityCodes
    (@BrandID INT,
    @CityCodes nvarchar(128),
    @ProductType nvarchar(32))

    AS SELECT * FROM WebSpecial
    WHERE (ProductType = @ProductType)
    AND (CityCode IN (@CityCodes))
    AND (SaleEndDate >= GetDate()
    AND TravelEndDate >= GetDate()
    AND BrandID = @BrandID)
    ORDER BY ItemPriority
    GO

    I've tried so many combinations of "AND (CityCode IN (@CityCodes))" that it's
    not funny. I've even rebuilt the list that is fed to the SP so that each item
    is wrapped in single quotes but to no avail.

    Has anyone else come across this? And if so, did you find a work around?

    Peace,

    Adam




    Adam72 Guest

  2. Similar Questions and Discussions

    1. Stored Procedures
      Hi all, I'm a little confused about how to obtain a result set from a stored procedure (stored in a Visual FoxPro 8.0 database) from an ASP.NET...
    2. dt_ Stored Procedures
      Please could you tell me if it is safe to remove the dt_ stored procedures from my database? I have spent some time searching the web/groups for...
    3. Simple Question: Converting lists to strings
      Is there a way to use a list in a case statement? Can I substitute myList = in the case statement below? case(myVar) of 1, 4, 6, 9, 11:...
    4. New to ASP and Stored Procedures
      Hi I have some experince with ASP and databases in General, however Stored Procedures are new. I need to call a stored procedure and have bene...
    5. Stored Procedures and 4GL
      Hello, I am using Informix 7 se database. Is it possible to call a 4GL program from a stored procedure? Thanks Ahmer
  3. #2

    Default Re: SQL Stored Procedures and Lists of Strings

    You don't have any quotes around your list items. The cf function ListQualify might help you.
    Dan Bracuk Guest

  4. #3

    Default Re: SQL Stored Procedures and Lists of Strings

    You have to run dynamic SQL statements, like :

    EXEC('SELECT * FROM table WHERE column IN (' + @varWithList +')')

    or the for SQL 2000 and newer (optimized command)

    DECLARE @SQLString Nvarchar(4000)
    SET @SQLString = 'SELECT * FROM table WHERE column IN (' + @varWithList +')'
    EXECUTE sp_executesql @SQLString

    Regards

    Sojovi Guest

  5. #4

    Default Re: SQL Stored Procedures and Lists of Strings

    Thanks Sojovi. This seems to deal with the list of strings ok but now i've run
    into something else that i can't work out.

    My procedure definition is (simplified for this purpose);

    CREATE PROCEDURE DSCC (@BrandID int)
    AS
    DECLARE @SQLString AS nvarchar(4000)
    SET @SQLString = 'SELECT * FROM WebSpecial WHERE BrandID = ' + @BrandID
    EXEC(@SQLString)
    GO

    But now, when i try to call this via CF, i get an error:

    "Syntax error converting the varchar value &apos;SELECT * FROM WebSpecial
    WHERE BrandID = &apos; to a column of data type int. "

    If i put it back to straight inline SQL as such;

    CREATE PROCEDURE DSCC (@BrandID int)
    AS
    SELECT * FROM WebSpecial WHERE BrandID = @BrandID
    GO

    It works fine and returns all records.

    Any ideas?

    Thanks. Adam

    Adam72 Guest

  6. #5

    Default Re: SQL Stored Procedures and Lists of Strings

    Thanks Dan. I did try it with single quotes wrapping each element but that
    didn't work either. I didn't know about the ListQualify function though,
    that's a much better way to do it than i had. Cheers.

    Adam72 Guest

  7. #6

    Default Re: SQL Stored Procedures and Lists of Strings

    When you run dynamic SQL statements, you can concatenate only strings. In your
    case, @BrandID is an integer, so if you want to concatenate the value in the
    SQL statement you have convert it into a string.
    You can do one of these choices :

    1) SET @SQLString = 'SELECT * FROM WebSpecial WHERE BrandID = ' + STR(@BrandID)
    OR
    2) SET @SQLString = 'SELECT * FROM WebSpecial WHERE BrandID = ' +
    CAST(@BrandID AS NVARCHAR) //OR VARCHAR OR CHAR OR NCHAR, there's no difference
    in the result, but for SQL are differents data types.

    Regards

    Sojovi Guest

  8. #7

    Default Re: SQL Stored Procedures and Lists of Strings

    Thanks Sojovi! Fixed it straight up!

    Thanks for your help.
    Adam72 Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139