Why Sql not bringing back results unless I set varchar size?
        Posted  
        
            by Tom
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Tom
        
        
        
        Published on 2010-06-17T08:03:52Z
        Indexed on 
            2010/06/17
            8:13 UTC
        
        
        Read the original article
        Hit count: 417
        
I've got an SQL script that fetches results based on the colour passed to it, but unless I set the size of the variable defined as a varchar to (50) no results are returned.
If I use: like ''+@Colour+'%' then it works but I don't really want to use it in case it brings back results I don't need or want.
The column FieldValue has a type of Varchar(Max) (which can't be changed as this field can store different things). It is part of aspdotnetstorefront package so I can't really change the tables or field types.
This doesn't work:
declare @Col VarChar
set @Col = 'blu'
select * from dbo.MetaData as MD where MD.FieldValue = @Colour
But this does work:
declare @Col VarChar (50)
set @Col = 'blu'
select * from dbo.MetaData as MD where MD.FieldValue = @Colour
The code is used in the following context, but should work either way
<query name="Products" rowElementName="Variant">
    <sql>
      <![CDATA[
            select * from dbo.MetaData as MD where MD.Colour = @Colour      
        ]]>
    </sql>
    <queryparam paramname="@ProductID" paramtype="runtime" requestparamname="pID" sqlDataType="int" defvalue="0" validationpattern="^\d{1,10}$" />
        <queryparam paramname="@Colour" paramtype="runtime" requestparamname="pCol" sqlDataType="varchar" defvalue="" validationpattern=""/>
  </query>
Any Ideas?
© Stack Overflow or respective owner