Solved

UPDATE query with nested SELECT statements

Posted on 2006-11-11
8
39,762 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 21

Expert Comment

by:pinaldave
ID: 17923365
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
ID: 17923367
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
ID: 17923381
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17923548
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
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 17923562
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
ID: 17923566
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
ID: 24318654
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
ID: 24318664
Didn't mean to comment in here, wrong window. Sorry!
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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