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.
| Author |
Topic |
|
prettyjenny
Yak Posting Veteran
57 Posts |
Posted - 2011-06-21 : 10:20:17
|
| Hello,I have two tables as below.I tried to create a query to replace the values of Student.Dept_name from Dept.Dept_Name where there is same Dept_ID.Here is my query:UPDATE StudentSET Student.Dept_Name = Dept.Dept_NameWHERE Student.Dept_ID = Dept.Dept_IDDeptDept_ID Dept_Name1 A12 B13 C14 A15 C16 B1 StudentID Dept_ID Dept_Name1 2 A2 3 A3 2 A4 1 A5 3 A As a result, Student table should have this data after I run the query:ID Dept_ID Dept_Name1 2 B12 3 C13 2 B14 1 A15 3 C1I got this error:The column prefix 'Dept' does not match with a table name or alias name used in the query.can you help?Thanks. |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-06-21 : 10:29:16
|
You have to include the Dept table, for example:UPDATE StudentSET Student.Dept_Name = Dept.Dept_NameFROM Student INNER JOIN DeptOn Student.Dept_ID = Dept.Dept_ID |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-21 : 10:29:27
|
| UPDATE StudentSET Dept_Name = Dept.Dept_Namefrom Studentjoin Depton Student.Dept_ID = Dept.Dept_IDYours would almost work with a from statement but I prefer this syntax.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
prettyjenny
Yak Posting Veteran
57 Posts |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-06-21 : 17:20:05
|
| >> I have two tables as below. <<People cannot read your mind, so post your code and clear specs if you really want help. Please post real DDL and not narrative or your own personal programming language. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. Please tell us what SQL product and release you are using. Tell us if you can change the DDL or if you are stuck with it. Do you really have only one department and one student. Likey0ou said when you used singular names? Your whole data model is wrong; a department is not an attribute of a student; a student and a department have a relationship since they are distinct, separate entities. The idea of a magical generic “id” is absurd.CREATE TABLE Departments(dept_id INTEGER NOT NULL PRIMARY KEY, dept_name VARCHAR(20) NOT NULL, ..);CREATE TABLE Students(student_id INTEGER NOT NULL PRIMARY KEY, student_name dept_name VARCHAR(20) NOT NULL, ..);Now we have to guess from your lack of specs if this is 1:1, 1:m or a n:m relationship. Read these DDLs until you understand themCREATE TABLE Enrollment(dept_id INTEGER NOT NULL UNIQUE REFERENCES Departments(dept_id), student_id INTEGER NOT NULL REFERENCES Students(student_id));CREATE TABLE Enrollment(dept_id INTEGER NOT NULL REFERENCES Departments(dept_id), student_id INTEGER NOT NULL UNIQUE REFERENCES Students(student_id));CREATE TABLE Enrollment(dept_id INTEGER NOT NULL REFERENCES Departments(dept_id), student_id INTEGER NOT NULL REFERENCES Students(student_id));>> I tried to create a query to replace the values of Students.dept_name [sic] from Departments.dept_name where there is same dept_id. <<An UPDATE is a statement and not a query. You seem to need more help tha nyou can get on the Internet; might want to get some basic books. If you want to see the names, then do a simple VIEW with three tables. Left as an exercise for the student :)--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
|
|
|
|
|