Solved

UPDATE query with nested SELECT statements

Posted on 2006-11-11
8
39,403 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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

803 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