Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2004

    Unanswered: how to update if customer doesn't exist (was "Help with an update")

    This is an Update that I have working, But what do I do, if the customer does not exist already it doesn't add the customer? How should I remedy this? if the customer does exist works great.

    SET CustId = Left (CustomerId,10),
    CustName = Left (CustomerName,25),
    Addr1 = Left (Address1,25),
    Addr2 = Left (Address2,25),
    City = Left (ca.City,15),
    Region = Left (State,2),
    PostalCode = Left (Zip,5)
    FROM RIO.dbo.tblArCust AC INNER JOIN
    (SELECT CustomerCode, MAX(LastUpdatedDate) MaxDate
    FROM COFFEE.dbo.vueCustomerAddress
    GROUP BY CustomerCode) V
    ON V.CustomerCode = AC.CustId
    INNER JOIN COFFEE.dbo.vueCustomerAddress CA
    ON CA.CustomerCode=V.CustomerCode AND
    WHERE CA.addresstypeid = 1

  2. #2
    Join Date
    Apr 2002
    Toronto, Canada
    guess it depends how often you expect a customer not to exist compare to how often you expect to have to update an existing customer

    this will determine whether you update and then insert, or insert and then update, assuming in each case that you would interpret the error message issued by the first statement to determine whether to run the second

    you want the one that has the greater likelihood of success to come first, so that you can skip the second | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts