Solved

UPDATE query with nested SELECT statements

Posted on 2006-11-11
8
39,850 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

624 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