Solved

ms sql  - incorrect syntax issue with update statement

Posted on 2010-09-20
9
224 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
 
LVL 41

Expert Comment

by:ralmada
ID: 33719562
the result is the same, just that filter was redundant.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 68

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 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

19 Experts available now in Live!

Get 1:1 Help Now