views:

36

answers:

3

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=""/>

+1  A: 

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.

hgulyan
I don't set the size dynamically its done by somwething in aspdnsf
Should be able to sort it now. Thanks for the help.
You're welcome.
hgulyan
A: 

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

Barry
Why do you write the same thing?
hgulyan
Um because that is the answer? I have answered it just before you. Unfortunately I can't tell whether someone else is in the middle of writing the correct answer.
Barry
@Barry, just after me.
hgulyan
@hgulyan - apologies, just after you.
Barry
+6  A: 

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

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

Ardman
Ahh rite thats why it won't work. The xml.config page is meant to set the varchar size dynamically, so in the case of blu it would be 3. but it still does not work.
@Tom: Isn't your xml.config file static? How is this supposed to change the value dynamically?
Ardman
It's done via a asp.net engine that's part of asdnsf that passes the colour and sets it in the @Colour param
Thinks it must be something to do with the package passing it to the sql. Should be able to sort it now. Thanks for the help.