Unix Technical Forum

Deleting data by comparing to another table

This is a discussion on Deleting data by comparing to another table within the SQL Server forums, part of the Microsoft SQL Server category; --> I have an entry form allowing customers to enter up to 15 skus (product id) at a time, so ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:56 PM
Bill
 
Posts: n/a
Default Deleting data by comparing to another table

I have an entry form allowing customers to enter up to 15 skus (product
id) at a time, so they can make a multiple order, instead of entering
one sku, then submitting it, then returing to the form to submit the
second one, and so forth.

From time to time, the sku they enter will be wrong, or discontiued, so
it will not submit an order.

Therefore, when they are done submitting their 15 skus through the order
form, I want a list showing them all of those skus that came back blank,
or were not found in the database.

I'm doing this by creating two tables. A shopping cart, which holds all
the skus that were returned, and a holding table, that holds all the
skus that were submitted. I want to then delete all the skus in the
holding page that match the skus in teh cart (because they are good
skus) which will then leave the unmatched skus in the holding table.
I'll then scroll out the contents of the holding table, to show them the
skus that were not found in the database.
(confused yet?)
So what I want to do is have some sql that will delete from the holding
table where the sku = the sku in the cart. I've tried writing this, but
it dosn't work.

I tiried this delete from holding_table where sku = cart.sku

I was hoping this would work, but it dosn't. Is there a way for me to do
this?

Thanks!

Bill


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:56 PM
David Portas
 
Posts: n/a
Default Re: Deleting data by comparing to another table

Let me suggest that you have the wrong design to start with. Creating
shopping cart tables and holding tables for each user is not a very
effective design and it doesn't make sense in relational terms.

Create a table for all your shopping carts with an ID for each user/session:

CREATE TABLE ShoppingCarts (cart_id INTEGER, sku INTEGER NOT NULL /*
REFERENCES Products (sku) */, PRIMARY KEY (cart_id,sku))

I would have thought it made sense to validate the SKUs as they are entered
which is what the commented FK constraint does in the above code. If you
want to post-validate the SKUs, leave the FK constraint out and display the
invalid entries with this query (@cart_id is the id for this user/session):

SELECT C.sku AS Invalid
FROM ShoppingCarts AS C
LEFT JOIN Products AS P
ON C.sku=p.sku
WHERE C.cart_id = @cart_id AND P.sku IS NULL

You can delete the invalid SKUs like this:

DELETE
FROM ShoppingCarts
WHERE cart_id = @cart_id
AND NOT EXISTS
(SELECT *
FROM Products
WHERE sku = ShoppingCarts.sku)

--
David Portas
------------
Please reply only to the newsgroup
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:57 PM
Bill
 
Posts: n/a
Default Re: Deleting data by comparing to another table

AWESOME, David! Thank you.

Bill

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:02 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com