Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello folks.

Can anyone tell me what is wrong with the following SQL statements?

The purpose purpose of the UPDATE is to promote one of the prod_cat_code values for a product that are stored in the product_category_xref table up to the product table (any value will be good - even NULL). So I have:
SQL
UPDATE p 
SET    p.prod_cat_code = (SELECT TOP(1) pcx.cat_code 
                          FROM          prod_category_xref pcx 
                          WHERE         pcx.prod_num = p.prod_num) 
FROM   product p; 

After that, I want to get rid any records in product_category_xref which have the same prod_cat_code as the one now stored on the product record.
SQL
DELETE pcx 
FROM   [dbo].[product_category_xref] pcx 
WHERE  EXISTS (SELECT [dbo].[product] AS p 
               WHERE  p.prod_num = pcx.prod_num 
                      AND p.prod_cat_code = pcx.prod_cat_code);

However, when I execute these two statements I get errors for the DELETE:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "p.prod_num" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "p.prod_cat_code" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "dbo.product" could not be bound.


I don't understand why. I eventually changed the DELETE to
SQL
DELETE pcx 
FROM   product_category_xref pcx 
       JOIN product AS p 
            ON p.prod_num = pcx.prod_Num 
               AND p.prod_cat_code = pcx.prod_cat_code;

which accomplishes the same thing. But now I get an error on the UPDATE (which didn't throw an error before):
Msg 208, Level 16, State 1, Line 9
Invalid object name 'prod_category_xref'.

All I can say is 'huh?' I know that I can change the UPDATE to work too, but I don't see why the original statements do not.
Any ideas?
Posted
Updated 25-Jul-14 9:57am
v2
Comments
Clive D. Pottinger 25-Jul-14 16:02pm    
Okay - sometimes all it takes is trying to tell someone else about a problem in order to find an answer (or in this case, a partial answer).
The syntax errors with the UPDATE are because I mistyped the table and column names. The query should be:
UPDATE p
SET p.prod_cat_code = (SELECT TOP(1) pcx.prod_cat_code
FROM [dbo].[product_category_xref] pcx
WHERE pcx.prod_num = p.prod_num)
FROM [dbo].[product] p;

But I still don't know what is wrong with the DELETE

Look here[^]. Example can be found
 
Share this answer
 
Comments
Clive D. Pottinger 25-Jul-14 16:45pm    
Thanks for the reply digimanus, but I don't see how that link relates to this question. Perhaps if I rephrase: I am not not looking for a way to perform the task - I can figure that out. What I want to know is why SQL does not like my DELETE statement - why won't it bind the columns and table? What is the error I made?
Herman<T>.Instance 25-Jul-14 17:40pm    
Because your update or delete does not know what is in the subselect. The subselects only give an answer. By JOINing the tables the query sees all.
Clive D. Pottinger 25-Jul-14 18:22pm    
AAAH ! ! !
It took a lot of very hard staring before I finally saw what you meant. I am not sure I can put it in words, but I do understand (vaguely).
Maciej Los 28-Jul-14 18:12pm    
Good point! +5!
See digimanus[^] answer to get proper update statement.
If error occurs on delete statement, you need to check if ON CASCADE DELETE is on[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900