Kayser
Kayser

Reputation: 6694

Why are the results different in these two sql's

I try to update a column in an table with the following query. In that case 32000 Rows are updated.

Update Table72D012 t2 set DEFINING_ARGUMENT_ID = 
(
SELECT   t3.ID
FROM     Table70t3 t3,
         Table70t1 t1,
         Table70t0 t0,         
         Table4 t4
WHERE    t2.Model_id = t4.ID
AND      t3.ID = t1.PARAM_VALUE_BPVA_ID
AND      t3.VALUE_BPAR_ID = t0.ID
AND      t0.KEY='SPECIAL_KEY'
AND      t1.LANGUAGE='en'
AND      t4.Model = t1.name

);

I wanted check result and I used the following query to get the count of relevant ids. I got the result 26500.. Why are the result (count of update and count of select result) different from each other ?
How can I correct the UPDATE query?

ONLY SELECT

SELECT   t3.ID
FROM     Table70t3 t3,
         Table70t1 t1,
         Table70t0 t0,
         Table72D012 t2,
         Table4 t4
WHERE    t2.Model_id = t4.ID
AND      t3.ID = t1.PARAM_VALUE_BPVA_ID
AND      t3.VALUE_BPAR_ID = t0.ID
AND      t0.KEY='SPECIAL_KEY'
AND      t1.LANGUAGE='en'
AND      t4.Model = t1.name

Upvotes: 0

Views: 67

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270773

First, you need to learn the correct join syntax. Doing the joins in the WHERE clause is a bad, bad idea.

The reason is simple. The first query is updating very row in the table, because you have no WHERE clause or other condition.

The second query is returning all the rows that match the query. In the update version, those rows will get values of NULL.

This is the nice syntax, supported by MS SQL but not by DB2:

Update Table72D012 t2
    set DEFINING_ARGUMENT_ID = t3.ID
    from (SELECT t4.id as t4_id, t3.ID
          FROM Table70t3 t3 join 
               Table70t1 t1
               on t3.ID = t1.PARAM_VALUE_BPVA_ID join
               Table70t0 t0
               on t3.VALUE_BPAR_ID = t0.ID join
               Table4 t4 
               on t4.Model = t1.name
          WHERE t0.KEY='SPECIAL_KEY' AND t1.LANGUAGE='en'
          ) t
    WHERE t2.Model_id = t4_ID

I don't think DB2 allows the FROM clause in an UPDATE, so you need something like this:

Update Table72D012 t2
    set DEFINING_ARGUMENT_ID = 
         (SELECT t3.ID
          FROM Table70t3 t3 join 
               Table70t1 t1
               on t3.ID = t1.PARAM_VALUE_BPVA_ID join
               Table70t0 t0
               on t3.VALUE_BPAR_ID = t0.ID join
               Table4 t4 
               on t4.Model = t1.name
          WHERE t0.KEY='SPECIAL_KEY' AND t1.LANGUAGE='en' and t2.Model_id = t4.ID
          ) 
    WHERE exists              
         (SELECT t3.ID
          FROM Table70t3 t3 join 
               Table70t1 t1
               on t3.ID = t1.PARAM_VALUE_BPVA_ID join
               Table70t0 t0
               on t3.VALUE_BPAR_ID = t0.ID join
               Table4 t4 
               on t4.Model = t1.name
          WHERE t0.KEY='SPECIAL_KEY' AND t1.LANGUAGE='en' and t2.Model_id = t4.ID
          ) 

Upvotes: 3

Related Questions