Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way is to use the <code>OUTPUT</code> clause; in this case, you could select all address book ID's you've inserted, and use that to update the campaigns. Still, you'd really need to use transactions if you want to get this reliable, with a lock that will prevent <strong>both updates and inserts</strong>.</p> <pre><code>DECLARE @addressBookIds TABLE(AddressBookId INT NOT NULL) INSERT INTO TBL_RECIPIENTS OUTPUT INSERTED.adds_ABMId INTO @addressBookIds SELECT TBL_ADDRESSBOOKADDRESSES.* FROM TBL_ADDRESSBOOKADDRESSES INNER JOIN TBL_CAMPAIGNS ON TBL_ADDRESSBOOKADDRESSES.adds_ABMId = TBL_CAMPAIGNS.campaign_AddressBook WHERE TBL_CAMPAIGNS.campaign_Status = 1 UPDATE TBL_CAMPAIGNS SET [campaign_Status] = 2 FROM TBL_CAMPAIGNS INNER JOIN @addressBookIds AS T ON TBL_CAMPAIGNS.campaign_AddressBook = T.AddressBookId AND TBL_CAMPAIGNS.campaign_Status = 1 </code></pre> <p>If the campaign ID was part of <code>TBL_RECIPIENTS</code> table, you'd be 100% certain you've got the right campaign ID's through the <code>OUTPUT</code> clause and locking transactions would not really be required. As far as I can tell, SQL Server does not allow you to refer to columns from joined tables in the <code>OUTPUT</code> clause (unlike <code>DELETE ... OUTPUT</code> and <code>UPDATE ... OUTPUT</code>), so the campaign ID <em>must</em> be part of the <code>INSERT</code>'s <code>SELECT</code> clause. </p> <p>Because the campaign ID is not part of the output, this solution is pretty much a complicated version of HLGEM's answer, but it could be useful to you in the future.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload