Solved

How do I convert this SQL SELECT into an UPDATE

Posted on 2016-09-15
10
32 Views
Last Modified: 2016-09-15
I would like to update the Requested_Date on all the records returned by this SQL statement to 9/19/16.

SELECT        dbo.Delivery.Promised_Date
FROM            dbo.Job LEFT OUTER JOIN
                         dbo.Delivery ON dbo.Job.Job = dbo.Delivery.Job
WHERE        (dbo.Job.Status <> 'template') AND (dbo.Job.Customer_PO = '114672') AND (dbo.Delivery.Requested_Date = CONVERT(DATETIME, '2016-09-15 00:00:00', 102))

Open in new window


How would I write that considering I am doing a JOIN.

Thanks in advance for your help.
0
Comment
Question by:princeservice
  • 5
  • 5
10 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41799986
Give this a whirl.  Note the use of table aliases j and d for easier readability, use of indentation, and elimination of unneeded parentheses marks
UPDATE j
SET j.Promised_Date = '20160919'
FROM dbo.Job j
   LEFT JOIN dbo.Delivery d ON j.Job = d.Job
WHERE 
   j.Status <> 'template' AND 
   j.Customer_PO = '114672' AND 
   d.Requested_Date = CONVERT(DATETIME, '2016-09-15 00:00:00', 102)

Open in new window

0
 

Author Comment

by:princeservice
ID: 41800063
I added an addition to the WHERE clause to only change one record for the test (Line 9). Requested_Date was supposed to be Promised_Date but that was my bad (Line 8). Also Promised_Date is in the Delivery table, not Job table (Line 2). So here is the adjusted code:

UPDATE j
SET d.Promised_Date = '20160919'
FROM dbo.Job j
   LEFT JOIN dbo.Delivery d ON j.Job = d.Job
WHERE 
   j.Status <> 'template' AND 
   j.Customer_PO = '114672' AND 
   d.Promised_Date = CONVERT(DATETIME, '2016-09-15 00:00:00', 102) AND
   j.Job = '105865'

Open in new window


I get the following error in SSMS:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "d.Promised_Date" could not be bound.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41800069
Explain what you're trying to do with the WHERE .. d.Promised_Date line.  Is it a datetime or varchar?  Guessing you intended to have a column as the second parameter and not a hard coded date?  CONVERT in this manner converts to a varchar, so not sure why you have datetime there.  

Check out SQL Server Date Styles (formats) using CONVERT() for some help with that expression.
0
 

Author Comment

by:princeservice
ID: 41800076
Promised date is a datetime type. When I go into SSMS and create a view this is the SELECT statement it creates.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41800081
Yeah that seems odd, as you shouldn't be formatting values to shove into a datetime column.  I'd work with that to make sure it's what you need, as it's not real clear to me what you're trying to pull off there.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:princeservice
ID: 41800186
I'm confused as to why it's not real clear. I just want to mass change all the dates based on the results returned by my original query.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41800201
<correction>

Looking at the code block in my above comment, it appears the error was because of the SET statement with the d. table alias and not the WHERE clause.  So correcting the table alias..

UPDATE j
SET j.Promised_Date = '20160919'
FROM dbo.Job j
   LEFT JOIN dbo.Delivery d ON j.Job = d.Job
WHERE 
   j.Status <> 'template' AND 
   j.Customer_PO = '114672' AND 
   d.Promised_Date = CONVERT(DATETIME, '2016-09-15 00:00:00', 102) AND
   j.Job = '105865'

Open in new window

0
 

Author Comment

by:princeservice
ID: 41800211
Promised date is in the delivery table
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41800216
Okay, then switch the FROM and JOIN, and revisit the use of LEFT JOIN in the query..

UPDATE d
SET d.Promised_Date = '20160919'
FROM dbo.Delivery d 
   LEFT JOIN dbo.Job j ON j.Job = d.Job
WHERE 
   j.Status <> 'template' AND 
   j.Customer_PO = '114672' AND 
   d.Promised_Date = CONVERT(DATETIME, '2016-09-15 00:00:00', 102) AND
   j.Job = '105865'

Open in new window


You may have to make minor modifications to get this to work, as experts here cannot connect to your data source(s) and run queries, so we are limited to reading T-SQL in questions and making educated guesses.
0
 

Author Closing Comment

by:princeservice
ID: 41800246
Worked perfect. Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax using Declare 4 38
SQL 2008 Conversion failed 7 20
email the result out from a T-SQL queries 29 63
Parsing out characters on the right side of hyphen 3 23
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

23 Experts available now in Live!

Get 1:1 Help Now