Friday, March 30, 2012

Problem with SQL Statement

Can anyone see anything obviously wrong with this statement? I'm not really
an SQL kind of guy

SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND CategoryID
= 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title

PlatformID contains a GUID and I thought passing a wildcard would allow all
all records to be selected with the matching CategoryID

Any ideas? I'm assuming I've just formed the SQL incorrectly"Andrew Banks" <banksy@.nojunkblueyonder.co.uk> wrote in message
news:iju_b.555$1d5.5476581@.news-text.cableinet.net...
> Can anyone see anything obviously wrong with this statement? I'm not
really
> an SQL kind of guy
> SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND
CategoryID
> = 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title
> PlatformID contains a GUID and I thought passing a wildcard would allow
all
> all records to be selected with the matching CategoryID
> Any ideas? I'm assuming I've just formed the SQL incorrectly

The LIKE is the problem - you can look it up in Books Online - but it
doesn't seem to be necessary here anyway, as your description suggests that
the PlatformID is irrelevant:

SELECT
ProductID, Title
FROM
Products
WHERE
CategoryID = 'f5e45b55-95de-47f4-a79b-b24969178b52'
ORDER BY
Title

If this doesn't give the results you expect, then you'll need to post some
more information about the structure of the table and what your data look
like.

Simon|||Managed to Fix it simon.

I need the PlatformID as users are selecting both the platform and category
from a drop down menu.

1 platform can have many categories

"Simon Hayes" <sql@.hayes.ch> wrote in message
news:403a7398$1_1@.news.bluewin.ch...
> "Andrew Banks" <banksy@.nojunkblueyonder.co.uk> wrote in message
> news:iju_b.555$1d5.5476581@.news-text.cableinet.net...
> > Can anyone see anything obviously wrong with this statement? I'm not
> really
> > an SQL kind of guy
> > SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND
> CategoryID
> > = 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title
> > PlatformID contains a GUID and I thought passing a wildcard would allow
> all
> > all records to be selected with the matching CategoryID
> > Any ideas? I'm assuming I've just formed the SQL incorrectly
> The LIKE is the problem - you can look it up in Books Online - but it
> doesn't seem to be necessary here anyway, as your description suggests
that
> the PlatformID is irrelevant:
> SELECT
> ProductID, Title
> FROM
> Products
> WHERE
> CategoryID = 'f5e45b55-95de-47f4-a79b-b24969178b52'
> ORDER BY
> Title
> If this doesn't give the results you expect, then you'll need to post some
> more information about the structure of the table and what your data look
> like.
> Simon|||Andrew Banks (banksy@.nojunkblueyonder.co.uk) writes:
> Can anyone see anything obviously wrong with this statement? I'm not
> really an SQL kind of guy
> SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND
> CategoryID
>= 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title
> PlatformID contains a GUID and I thought passing a wildcard would allow
> all all records to be selected with the matching CategoryID

First a general piece of advice: when you post a question like this, be
more specific. If there is an error message, please share it. If you
get unexpected results, please say so, and why they are unexpected.

In this particular case, the problem is that you cannot have a mod
operator between LIKE and AND. The percent is presumably meant to be
a wildcard, but in this case you need to quote it:

... LIKE '%' AND ...

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.aspsql

No comments:

Post a Comment