Question

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?

Also I can't set the size in <queryparam paramname="@Colour" paramtype="runtime" requestparamname="pCol" sqlDataType="varchar" defvalue="" validationpattern=""/>

Was it helpful?

Solution

The varchar declaration will give you 1 character by default unless you specify otherwise.

http://msdn.microsoft.com/en-us/library/ms176089.aspx

OTHER TIPS

It's because you declare @Col as varchar. It's the same as to right varchar(1).

When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.

Quote from here

http://msdn.microsoft.com/en-us/library/aa258242(SQL.80).aspx

UPDATE

Why do you need to specify varchar size dynamically? Just set it to max possible length.

Declare @Col varchar is essentially Declare @Col varchar(1) so when you assign the value the result will be that @col is equal to "b".

This behaviour is by design and is described in MSDN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top