Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Why doesn't my update statement work?

Author  Topic 

MikeMacairan
Starting Member

3 Posts

Posted - 2010-11-30 : 15:27:44
I have two tables and I'm trying to make a trigger on one, but my query doesn't work.
(table names have been changed to protect the innocent)

Department Table
dept_no dept_name
1 Science
2 Math
3 English



Student_Class Table
student_id dept_no dept_name
t 3 Department
u 3 Department
v 3 Department
w 2 Department
x 3 Department
y 1 Department
z 3 Department


I need a query that will update the department name in Student_Class to match the data in the Department table, but when I try the following:

update Student_Class set Student_Class.dept_name = Department.dept_name where Student_Class.dept_no = Department.dept_no

I get an error that reads: "The multi-part identifier Department.dept_name could not be bound"

I just want to make the dept_name field in Student_Class to match the dept_name in Department where the dept_no matches.

Can someone plese help?

Thanks.

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-30 : 15:36:01
You get the error because the Department table is not referenced anywhere. You need the UPDATE ... FROM style of update statement. Check Books Online (the SQL help file) for full details.

p.s. This is a bad design. Department name has no business in the student table. It's a violation of 3rd normal form.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-30 : 15:47:59
Here's an example:

UPDATE t
SET c2 = o.c2
FROM SomeTable t
JOIN SomeOtherTable o
ON t.c1 = o.c1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-30 : 15:48:53
Oh and I agree that you design is flawed. You should not have repeated data like that. Put the department name in the Department table and no where else.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

MikeMacairan
Starting Member

3 Posts

Posted - 2010-11-30 : 15:51:37
Thanks for the quick reply.

I'm not surprised that it is a bad design. The tables are not really the ones in the database I am using, but I was trying to make a crude example for the sake of asking my question.

I'm not a dba and am usually tasked only with querying the DB with very simple select queries to get answers, yet I have just been tasked with creating a trigger. My table (Student, as an example) gets data put into it but the dept_name will always have a generic value when the data arrives. I cannot control that; it's how the data comes to me.

What I need to do is create a trigger that changes dept_name to some other value based on it's dept_number, so I created a table with two fields, dept_no and dept_name. I just want to update the dept_name in Student to match the one in Department where the dept_no is the same.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-30 : 15:53:42
Do you need more than the example that I posted?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

MikeMacairan
Starting Member

3 Posts

Posted - 2010-11-30 : 16:01:53
tkizer,

Your example was perfect. I changed my query to match your design and it updated exactly as I needed it to.

Thank you all for your quick and helpful responses.

Mike
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-30 : 16:02:59
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -