I have worked with SQL Server for more than 8 years, and I'm used to writing queries like this when I'm trying to update data based on query involving table joins:
update tableA
set col1 = 0
from tableA A
join tableB B on A.id = B.bid
where b.name = 'x'
So after years of writing this kind of sql, which comes so natural to me, imagine my surprise when I moved to Netezza and wrote a query like this to update some data and I get an error message that says:
ERROR [HY000] ERROR: Update canceled: attempt to update a target row with values from multiple join rows
Reading the message, it's trying to tell me that based on my join, I'm getting a one to many match on the rows I'm trying to delete from. So? SQL Server never complained about it. There are many cases where we do a join on tables with 1 to many relationships and do deletes/updates.
But Netezza doesn't like it. You can of course rewrite the query to have an IN clause. But there's also another funky way (at least funky to me) to achieve the desired results:
update tableA A
set col1 = 0
from tableB B
where A.id = B.bid
and b.name = 'x'
For a 3 table join:
update tableA f
set msid = 0
from tableB d, tableC t
where A.p_sid = D.d_sid
and A.t_sid = t.t_sid
and D.day_start = '2012-01-03'
and T.time_minute = 10
Well, I am in the same case... first time I work on Netezza... I have to adapt a DB2 loading script into Netezza stored procedure... and it is just amazing the difference about UPDATE... Thanks a lot to share your experience
ReplyDeleteYes, I've been using Netezza for about 2 years now and for the most part it's been great (and fast!). Every now and then small things like this update statement still trips me up but I can live with it.
ReplyDeleteWhy not just use distinct on table B?:
ReplyDeleteupdate tableA
set col1 = 0
from tableA A
join (SELECT DISTINCT bid FROM tableB WHERE name = 'x') B
on A.id = B.bid
Scratch that. I just tried it, and the error is because I have multiple rows on the table I'm trying to update. Apparently, Netezza update statements don't like to use JOIN syntax. I guess we'll just have to use the older syntax for joins in updates.
Deletecreate temp table customer_all_update as
ReplyDeleteselect b.* ,
row_number() over(partition by customer_dim_id order by customer_dim_id) as upd_cntr
from customer_all a, customer_dex b
where a.customer_dim_id =b.customer_dim_id;
update customer_all a
set customer_company_cd = b.customer_company_cd
from customer_dex b
where a.customer_dim_id =b.customer_dim_id and upd_cntr=1;
update tableA A
ReplyDeleteset col1 = 0
from tableB B
where A.id = B.bid
and b.name = 'x'
I used the above syntax and still getting the same error . any suggestions would be appreciated.
Thanks,
Brajendra
Great Post! Worked for me!
ReplyDeleteit worked for me, thank you
ReplyDelete@Brajendra, in your query shown, use set A.col1 = 0 (You missed the A. reference for col1)
ReplyDeleteThanks for give me this information really this product is very effective.
ReplyDeleteNexus 5 Phone Cases and Covers
Thanks alot...It Worked for me
ReplyDeleteI need below thing exact in Netezza , how it will be in netezza. can anyone share the script?
ReplyDeleteBEGIN TRANSACTION A
SELECT 'BEFORE UPDATE', * FROM Table
UPDATE Table
SET column_1= REPLACE(column_1, '_', ' ')
SELECT 'AFTER UPDATE', * FROM Table
ROLLBACK TRANSACTION A
Great post. I have also worked in sql server for about 6 years and just moved to netezza and the syntax was a headache initially.
ReplyDeleteIts pretty fast in my opinion and you would love it if you just know the proper syntax.
goruntulu show
ReplyDeleteücretli
MY7H
whatsapp görüntülü show
ReplyDeleteücretli.show
FZ7K