Solved

ms sql  - incorrect syntax issue with update statement

Posted on 2010-09-20
9
227 Views
Last Modified: 2012-05-10
getting an error with my update statement:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'dd'.



update ss_date_example dd
join soda ss on dd.soda_id = ss.soda_id
join #temp tt on ss.juice_number = tt.juice_number
set dd.case_account_id = tt.case_desired_account
where tt.milk_id = dd.milk_id
and tt.juice_number = ss.juice_number
0
Comment
Question by:GlobaLevel
  • 4
  • 4
9 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 33719125
update dd
set dd.case_account_id = tt.case_desired_account
from ss_date_example dd
join soda ss on dd.soda_id = ss.soda_id
join #temp tt on ss.juice_number = tt.juice_number
where tt.milk_id = dd.milk_id
and tt.juice_number = ss.juice_number
0
 
LVL 41

Accepted Solution

by:
ralmada earned 250 total points
ID: 33719136
and actually you don't need the last filter, since you're already doing that in the join
update dd
set dd.case_account_id = tt.case_desired_account
from ss_date_example dd
join soda ss on dd.soda_id = ss.soda_id
join #temp tt on ss.juice_number = tt.juice_number
where tt.milk_id = dd.milk_id
 
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 33719406
the data doesnt change though correct?  does it change the data if I put it in the join or the where clause?
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 41

Expert Comment

by:ralmada
ID: 33719562
the result is the same, just that filter was redundant.
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 33720742
What ralmada tried to say: The join for the update is already giving you only records which are found in both  tables. Thus the additional restriction in the where clause is superfluous.

MSSQL does not allow to provide a join directly in the UPDATE clause. You can either do
update tbl1set ...from tbl2where tbl1.x = tbl2.x
or
update alias1set ...from tbl1 alias1 join tbl2 alias2 on ....
If you have something more complex to update, it is better to first create a (updatable) select for checking the results,
select dd.case_account_id, tt.case_desired_accountfrom ss_date_example dd join soda ss on dd.soda_id = ss.soda_idjoin #temp tt on ss.juice_number = tt.juice_number     and  tt.milk_id = dd.milk_id
and then use the select for update:
update x
set case_account_id = case_desired_account
from
(select dd.case_account_id, tt.case_desired_account
 from ss_date_example dd
 join soda ss on dd.soda_id = ss.soda_id
 join #temp tt on ss.juice_number = tt.juice_number
      and  tt.milk_id = dd.milk_id) x
That way you can be certain you do not update anything unwanted.
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 33720955
Can o run this update thru Asp.net to a ms SQL db 2005??? Thanks all
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 33720957
Can o run this update thru Asp.net to a ms SQL db 2005??? Thanks all
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33722145
certainly you can.
0
 
LVL 10

Author Closing Comment

by:GlobaLevel
ID: 33780257
thks
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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

810 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