How to check empty table in Where Condition
I want to update several columns of a table at the same time. And the value for each column is retrieved from several tables. My tables are:
Table : Customer
Columns are:
customerCode
kategori
design
designCode
value
Table Name : Customer_kategori
Columns:
customerCode
kategori
and similar .....
My sql query is:
UPDATE Customer
SET
kategori = Customer_kategori.kategori,
design = Customer_design.design,
designCode = Customer_designCode.designCode,
value = Customer_value.value
FROM
Customer_kategori,
Customer_design,
Customer_designCode,
Customer_value
WHERE
Customer_kategori.customerCode = Customer.customerCode
AND Customer_design.customerCode = Customer.customerCode
AND Customer_designCode.customerCode = Customer.customerCode
AND Customer_value.customerCode = Customer.customerCode
The columns are updated if all the tables (Customer_kategori, Customer_design, Customer_designCode, Customer_value) contains values. But if tables ar开发者_JAVA百科e empty, columns are not updated as Where condition is not satisfied.
I also try using CASE in WHERE Condition. My code is ,
WHERE
Customer_kategori.customerCode = CASE WHEN Customer_kategori.customerCoder IS NOT NULL THEN Customer.customerCode END
AND Customer_design.customerCode = CASE WHEN Customer_design.customerCode IS NOT NULL THEN Customer.customerCode END
AND Customer_designCode.customerCode = CASE WHEN Customer_designCode.customerCode IS NOT NULL THEN Customer.customerCode END
AND AND Customer_value.customerCode = CASE WHEN Customer_value.customerCode IS NOT NULL THEN Customer.customerCode END
But this does not work either. I also try to check the table, with following where clause,
WHERE
CASE WHEN (select count(Customer_kategori.customerCode) > 0 from Customer_kategori) THEN Customer_kategori.customerCode = Customer.customerCode END
But no success..
In short:
- I want to check if tables are empty in WHERE clause
- And if it is empty I just want to ignore checking columns of those table that are empty.
Or may be there is other better way .... Any suggestion is appreciated...
Thank you
Will this work?
WHERE
(
Customer_kategori.customerCode = Customer.customerCode
OR
0 = (select count(Customer_kategori.customerCode) from Customer_kategori)
)
AND
(
....
)
UPDATE
How about this:
UPDATE Customer
SET
kategori = Customer_kategori.kategori,
design = Customer_design.design,
designCode = Customer_designCode.designCode,
value = Customer_value.value
FROM
Customer left join Customer_kategori on Customer_kategori.customerCode = Customer.customerCode
left join Customer_design on Customer_design.customerCode = Customer.customerCode
left join Customer_designCode on Customer_designCode.customerCode = Customer.customerCode
left join Customer_value on Customer_value.customerCode = Customer.customerCode
精彩评论