Solved

UPDATE query with nested SELECT statements

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Location of Dynamics AX Service accounts in SQL 3 16
Dynamic SQL select query 4 38
Get Next number from Stored Procedure 8 23
SQL - Simple Pivot query 8 15
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

827 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