问题
I have used bulk update to sync datas between two tables as below
$sqlProc="
UPDATE cards
SET cards.card_no = t2.card_number,
cards.expiry_date=t2.expiry_date OUTPUT INSERTED.Id AS 'updated_id'
FROM cards
INNER JOIN card_temp t2 ON (cards.account_no = t2.account_number
AND cards.customer_name=t2.customer_name)
WHERE cards.is_disabled='N'";
debug($this->Request->query($sqlProc));
Above query will also return Primary key of updated records usingOUTPUT INSERTED.Id AS 'updated_id'
in sql server editor but when i debug the sql
debug($this->Request->query($sqlProc));
Then it return true
for successful query and false
for unsuccessful query.
Is there any idea to fetch updated_id
to array , so that i can use those ids to another table
回答1:
Well , I figured out finally . I created one intermediate table did the following
//inserted into temp_id (bridge table)
$sqlProc="insert into temp_id select * FROM (update cards
set cards.card_no = t2.card_number,cards.expiry_date=t2.expiry_date
OUTPUT INSERTED.Id AS 'updated_id'
from cards
inner join card_temp t2
on (cards.account_no = t2.account_number and cards.customer_name=t2.customer_name)
where cards.is_disabled='N'
) AS t";
$this->Request->query($sqlProc);
//fetch from intermediate table
$sqlSel="SELECT * FROM temp_id";
$arr=$this->Request->query($sqlSel);
//this array will fetch all updated id's
debug($arr);
$sqlDel="DELETE FROM temp_id";
$this->Request->query($sqlDel);
来源:https://stackoverflow.com/questions/15758065/getting-pk-of-updated-records-in-cakephp-sql-server