Skip to main content
Known Participant
February 26, 2007
Question

Problem with IN clause

  • February 26, 2007
  • 7 replies
  • 950 views
I thought the concept was straight forward but I cannot figure out how to write the code for a table where one field, Category, may have several values. Category is set as an nvarchar field and typical data in this field may be 2,3,8. Here is the code I have written that does not work. The user starts at an index page and selects a category by name which is then converted to the category number and added to the URL after the ?, i.e. <a href="Products/Overview1.cfm?CatNum=#CatNum#">#Category#</a>

Before I realized that some products would fall into more than one category, everything worked fine. Now I am trying to avoid having to duplicate a row with only the category being different. Thought it was easy but now I am really frustrated. Anyone able to help?


<CFIF ParameterExists(URL.CatNum)>
<cfquery name="GetProductOverview" datasource="#DS#" username="#User#" password="#PW#">
SELECT ProdID, ProdName, ImageFileName, ImageHeight, ImageWidth, Category, Active
FROM Products
WHERE Active = 'true'
AND Category IN '#URL.CatNum#'
ORDER BY CatalogNum asc
</cfquery>
<cfelse>
<cflocation url="../index.cfm">
</CFIF>

Thanks,
Warren
This topic has been closed for replies.

7 replies

Inspiring
February 28, 2007
When you created this table, what did you define as your primary key? If you define it as ProdId and Category, then when you attempt to assign the same category twice for the same item, you won't succeed. That would be good.

Regarding this, "Since I am trying to add products to the tables as quickly as possible, it is far easier and quicker to use the SQL Enterprise Manager than to use a script because some of the fields are the same or close to those from one product to another. "

there is a saying in computer programming, "do it fast, do it twice".
Known Participant
February 28, 2007
Hi Dan,

Now I am really confused. When I redid my tables to achieve 3NF I created a table with two fields, ProdID and Category. Each product has a unique ID. This number goes in the ProdID field. However, each product may belong to several categories. Therefore, multiple rows in the table will have the same ProdID value but different Category values.

While working in the SQL Enterprise Manager to take the data from my old table and put it in the new table I made an error and used the same Category twice for one of the products. I caught the error when I saw the image for the product appear twice on the website. So, I went back into the Enterprise Manager to change the value of the duplicate to the correct category. Enterprise Manager balked and would not let me make the change. I cannot make either of the fields a key field as I must be able to have a number of identical ProdID entries each with a different Category value but the Category value also is repeated many times for other products. Therefore, I could not assign a key field in the table. This is why I created an ID field as a key field even though I see no use for the data in the field at this time.

What am I missing and how would you address this problem? Since I am trying to add products to the tables as quickly as possible, it is far easier and quicker to use the SQL Enterprise Manager than to use a script because some of the fields are the same or close to those from one product to another. For example, the available colors of the products are sometimes the same as the colors of another product or nearly the same (one or more colors may not be available or are in addition) so I can use a copy and paste and then edit quickly. By the way, the site I am building is at www.recycledpromoproducts.com.

Thanks much. I am certainly willing and eager to learn to do it the right way.

Regards,
Warren
Known Participant
February 27, 2007
Hi Dan,

It turns out that the problem is documented with Microsoft. The work around is to use the query analyzer rather than to try to change the data using the SQL Enterprise Manager. I was also told that since I have repeating values in both columns of the table (each product can have multiple categories) I should add in a field that I assign as the key field to overcome the problem. I did and that works fine.

I think I have learned a valuable lesson that every table should have an unused key field if there is no other field with unique values.

Thanks for the assistance. I will try to hang around here periodically to both learn and hopefully answer someone else's question.

Regards,
Warren
Inspiring
February 27, 2007
quote:

Originally posted by: WrapperGuy
Hi Dan,

It turns out that the problem is documented with Microsoft. The work around is to use the query analyzer rather than to try to change the data using the SQL Enterprise Manager. I was also told that since I have repeating values in both columns of the table (each product can have multiple categories) I should add in a field that I assign as the key field to overcome the problem. I did and that works fine.

I think I have learned a valuable lesson that every table should have an unused key field if there is no other field with unique values.


I disagree with that approach. It allows you to create records that might be unique because of the extra field, but are, for any practical purpose, duplicates. This causes all sorts of problems.

Inspiring
February 27, 2007
What's the primary key of this table?
Inspiring
February 27, 2007
Another option would be an product_price table looking something like this,

product_id pk (fk to product)
min_quantity pk
max_quantity pk
unit_price

This allows you to change the price of one item only.

The sql would resemble

select somefields, unit_price * quantity_ordered as theprice
from product p join product_price pp on p.product_id = pp.product_id
where qty_ordered between min_quantity and max_quantity
Known Participant
February 27, 2007
Thanks much for the suggestion. I successfully wrote the query using all the tables and it works fine except that one of the products now appears twice because of an error I made in the ProductCategory table. This leads to my next question.

In the ProductCategory table which simply has the ProdID and Category number I tried to correct a mistake in which I had one ProdID repeated twice and in which each row should have had a different Category value. I mistakenly entered the same Category value in both rows. I tried to change one of the entries to a different Category value and got the error message: "Key column information is insufficient or incorrect. Too many rows were affected by update." Have you any idea what's going on?

Many thanks,
Warren
Inspiring
February 26, 2007
If you don't understand the Scarecrow's answer, google "normalized database", or "data modelling tutorial".
Inspiring
February 26, 2007
You need to redesign your db.

The "category" column should not contain a list.

The reason the query does not work is because you have (if you replace "category" with the example)

AND 2,3,8 IN (1,2,3)

quote:

Now I am trying to avoid having to duplicate a row with only the category being different.

You need another table, which will hold the record id and the category id.

This is called normalised database.

Ken
Known Participant
February 27, 2007
Thanks for the response. As you can tell I am pretty green at this although I have used SQL databases for quite some time, probably poorly. I did Google "Data Modelling Tutorial" and read it carefully.

I think I now have a close to 3NF structure. I have one table for Products without the category column, I have a table for categories that has just the category number and the category name and I have a table with just the ProdID and the Category number. This part I know is OK.

The question I now have is this. The price of the products is a function of the quantity purchased. A number of products have the same quantity/price values. Is it better form to construct 2 more tables, one with a price group ID and the quantities and prices
e.g., price group = 1, qty1 = 200, price1 = $2.50, qty2 = 500, price2 = $2.25, qty3 = 1000, price3 = 1.80
and a second table with the ProdID and the price group?

I'm afraid the next step, constructing the query to tie this all together is going to take a lot of head scratching but I don't want to proceed until I know whether I should leave the quantity/price information in the Products table or remove it as stated above. I appreciate your willingness to help me out and your patience.

Regards,
Warren