Solved

UPDATE query with nested SELECT statements

Posted on 2006-11-11
8
39,192 Views
Last Modified: 2011-08-18
I need to update a field of a table that is itself used in the query to determine what to update.  The only error I get out of SQL is a general syntax error.

UPDATE tblInvoice_Master SET tblInvoice_Master.PercentSalesTax =
SELECT
      TaxRate
FROM
      tblCaliforniaTaxRate
WHERE
      (City = (
            SELECT
                  tblAddress.City
            FROM
                  tblInvoice_Master AS M
            INNER JOIN
                  tblAddress ON M.fkBillingAddress = tblAddress.ID AND
                  M.fkBillingAddress = tblAddress.ID
            WHERE
                  (M.ID = 622)
            )
      )

I need to update the tax rate for each record in tblInvoice_Master where M.ID = tblInvoice_Master.ID.  I used the value 622 to test and make sure that my subquery is returning only one result.  Does anyone have any suggestions.  I'm clearly making this more complex than it needs to be.
0
Comment
Question by:DanLockwood
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 21

Expert Comment

by:pinaldave
Comment Utility
UPDATE tblInvoice_Master SET tblInvoice_Master.PercentSalesTax =
SELECT TOP 1
     TaxRate
FROM
     tblCaliforniaTaxRate
WHERE
     (City = (
          SELECT
               tblAddress.City
          FROM
               tblInvoice_Master AS M
          INNER JOIN
               tblAddress ON M.fkBillingAddress = tblAddress.ID AND
               M.fkBillingAddress = tblAddress.ID
          WHERE
               (M.ID = 622)
          )
     )
0
 
LVL 21

Expert Comment

by:pinaldave
Comment Utility
correction:
UPDATE tblInvoice_Master SET PercentSalesTax =
SELECT TOP 1
     TaxRate
FROM
     tblCaliforniaTaxRate
WHERE
     (City = (
          SELECT
               tblAddress.City
          FROM
               tblInvoice_Master AS M
          INNER JOIN
               tblAddress ON M.fkBillingAddress = tblAddress.ID AND
               M.fkBillingAddress = tblAddress.ID
          WHERE
               (M.ID = 622)
          )
     )
0
 

Author Comment

by:DanLockwood
Comment Utility
That seems like it would guarantee only one record in the subquery, but the question remains how to I map the subquery M.ID field to the UPDATE query tblInovice_Master.ID field; these are actually the same field just being used in more than one spot in the query.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I have no idea what you are doing, but the following will correct your syntax error:

UPDATE      tblInvoice_Master
SET      tblInvoice_Master.PercentSalesTax =
      (SELECT      TaxRate
      FROM      tblCaliforniaTaxRate
      WHERE   City = (
                  SELECT      tblAddress.City
                  FROM      tblInvoice_Master AS M
                            INNER JOIN tblAddress ON M.fkBillingAddress = tblAddress.ID
                              AND M.fkBillingAddress = tblAddress.ID
                      WHERE      M.ID = 622
                  )
      )
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
This may be a tad simpler:

UPDATE      M
SET      PercentSalesTax = t.TaxRate
From      tblInvoice_Master M
      Inner Join tblAddress a ON M.fkBillingAddress = a.ID
      Inner Join tblCaliforniaTaxRate t ON a.City = t.City
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Just to make sure the above solution is giving the right values, use this:

Select     M.*, t.TaxRate
From     tblInvoice_Master M
     Inner Join tblAddress a ON M.fkBillingAddress = a.ID
     Inner Join tblCaliforniaTaxRate t ON a.City = t.City
0
 
LVL 1

Expert Comment

by:bejhan
Comment Utility
I forgot the GROUP BY clause. Still the same problem though.
UPDATE ltblAllItems AS a SET CustomerLastActivity = (SELECT MAX(LastActivity) FROM ltblAllItems c WHERE a.CustomerID = c.CustomerID AND a.DebtType = c.DebtType GROUP BY c.CustomerID, c.DebtType);

Open in new window

0
 
LVL 1

Expert Comment

by:bejhan
Comment Utility
Didn't mean to comment in here, wrong window. Sorry!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now