Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How do I combine 2 updates?

Author  Topic 

Leggy
Starting Member

3 Posts

Posted - 2009-11-06 : 10:08:56
Hi, I need to simplify a stored procedure by combining 2 update statements before I submit it. Both statements are using the same tables, but are targeting different data within them.
statements :
1.
update ipd
set carton_type = null
from inpt_pkt_dtl ipd join inpt_pkt_hdr iph on ipd.pkt_ctrl_nbr = iph.pkt_ctrl_nbr
where iph.ord_type = 'SD' and iph.whse = @whse and iph.co = @co and iph.div = @div

update ipd
set carton_break_attr = pkt_seq_nbr
from inpt_pkt_dtl ipd join inpt_pkt_hdr iph on ipd.pkt_ctrl_nbr = iph.pkt_ctrl_nbr
and iph.ord_type = 'SD'
join item_master im on ipd.cust_sku = im.vendor_item_nbr and im.std_pack_qty != '1'
and (im.carton_type != 'UGL' or im.carton_type is null)
and (im.spl_instr_code_3 != 'SP' or im.spl_instr_code_3 is NULL)
where convert(int,ipd.orig_pkt_qty) % 12 = 0

2.update ipd
set carton_break_attr = pkt_seq_nbr
from inpt_pkt_dtl ipd join inpt_pkt_hdr iph on ipd.pkt_ctrl_nbr = iph.pkt_ctrl_nbr
and iph.ord_type = 'SD'
join item_master im on ipd.cust_sku = im.vendor_item_nbr and im.std_pack_qty = '1'
and (im.carton_type != 'UGL' or im.carton_type is null)
and (im.spl_instr_code_3 != 'SP' or im.spl_instr_code_3 is NULL)
where convert(int,ipd.orig_pkt_qty) % 2 = 0

Or is it easier to run 2 statements?
My SQL knowledge is not fantastic and will appreciate any replies.

Cheers, Andy

Leggy
Starting Member

3 Posts

Posted - 2009-11-06 : 10:11:00
Sorry, I pasted an extra statement at the top, the updates are:
1.
update ipd
set carton_break_attr = pkt_seq_nbr
from inpt_pkt_dtl ipd join inpt_pkt_hdr iph on ipd.pkt_ctrl_nbr = iph.pkt_ctrl_nbr
and iph.ord_type = 'SD'
join item_master im on ipd.cust_sku = im.vendor_item_nbr and im.std_pack_qty != '1'
and (im.carton_type != 'UGL' or im.carton_type is null)
and (im.spl_instr_code_3 != 'SP' or im.spl_instr_code_3 is NULL)
where convert(int,ipd.orig_pkt_qty) % 12 = 0
and iph.whse = @whse and iph.co = @co and iph.div = @div

2.

update ipd
set carton_break_attr = pkt_seq_nbr
from inpt_pkt_dtl ipd join inpt_pkt_hdr iph on ipd.pkt_ctrl_nbr = iph.pkt_ctrl_nbr
and iph.ord_type = 'SD'
join item_master im on ipd.cust_sku = im.vendor_item_nbr and im.std_pack_qty = '1'
and (im.carton_type != 'UGL' or im.carton_type is null)
and (im.spl_instr_code_3 != 'SP' or im.spl_instr_code_3 is NULL)
where convert(int,ipd.orig_pkt_qty) % 2 = 0
and iph.whse = @whse and iph.co = @co and iph.div = @div
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-06 : 10:19:24
IF the predicates and joins are the same

UPDATE ipd
SET Col1 = xxx
, Col2 = yyy

FROM

WHERE



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-11-06 : 10:28:20
quote:
Originally posted by Leggy

Sorry, I pasted an extra statement at the top, the updates are:
1.
update ipd
set carton_break_attr = pkt_seq_nbr
from inpt_pkt_dtl ipd join inpt_pkt_hdr iph on ipd.pkt_ctrl_nbr = iph.pkt_ctrl_nbr
and iph.ord_type = 'SD'
join item_master im on ipd.cust_sku = im.vendor_item_nbr and im.std_pack_qty != '1'
and (im.carton_type != 'UGL' or im.carton_type is null)
and (im.spl_instr_code_3 != 'SP' or im.spl_instr_code_3 is NULL)
where convert(int,ipd.orig_pkt_qty) % 12 = 0
and iph.whse = @whse and iph.co = @co and iph.div = @div

2.

update ipd
set carton_break_attr = pkt_seq_nbr
from inpt_pkt_dtl ipd join inpt_pkt_hdr iph on ipd.pkt_ctrl_nbr = iph.pkt_ctrl_nbr
and iph.ord_type = 'SD'
join item_master im on ipd.cust_sku = im.vendor_item_nbr and im.std_pack_qty = '1'
and (im.carton_type != 'UGL' or im.carton_type is null)
and (im.spl_instr_code_3 != 'SP' or im.spl_instr_code_3 is NULL)
where convert(int,ipd.orig_pkt_qty) % 2 = 0
and iph.whse = @whse and iph.co = @co and iph.div = @div




where(convert(int,ipd.orig_pkt_qty) % 12 = 0
OR
convert (int,ipd.orig_pkt_qty) % 2 = 0)

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

Leggy
Starting Member

3 Posts

Posted - 2009-11-06 : 10:35:27
Many thanks guys. Much appreciated.

Go to Top of Page
   

- Advertisement -