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 |
|
MrRay
Starting Member
28 Posts |
Posted - 2004-12-06 : 14:45:58
|
| I'm not so hot in SQL Stored Procedures yet so I need a hand with one.I've got two tables. One table (LMS_Users) has a unique UserID field and stores the user's DeptID. The other table (LMS_TestSessions) also contains the UserID and the DeptID, however the DeptID is TODAY's DeptID and will not change when the user changes departemnts, so I can't relate the two columns.How can I update the blank DeptID fields in LMS_TestSessions with the current DeptID values in LMS_Users?Here's what I've got so far (doesn't work):================================CREATE PROCEDURE [dbo].[LMS_TestSessionDeptIDUpdate] ASUPDATE LMS_TestSessionsSET LMS_TestSessions.DeptID = LMS_Users.DeptID FROM LMS_Users INNER JOIN LMS_TestSessionsON (LMS_Users.UserID=LMS_TestSessions.UserID)WHERE LMS_TestSessions.DeptID <> LMS_Users.DeptIDGO |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-12-06 : 16:02:19
|
| If you post the DDL of the tables, some sample data in the form of DML and what you expect to happen as far as results go, I'm sure we can hook you up...Brett8-) |
 |
|
|
MrRay
Starting Member
28 Posts |
Posted - 2004-12-06 : 17:43:20
|
quote: Originally posted by X002548 If you post the DDL of the tables, some sample data in the form of DML and what you expect to happen as far as results go, I'm sure we can hook you up...
Thanks Brett,The stuff is pretty basic.From table LMS_Users:USERID | DEPTID (plus other details)User01 | 2User02 | 2User03 | 1User04 | 3User05 | 2User06 | 3From table LMS_Sessions:USERID | DEPTID (plus other details)User06 | ______User02 | ______User03 | ______User02 | ______User05 | ______User06 | ______User05 | ______User01 | ______etc.I need to run a one-time stored procedure to put the current DEPTID values for each user in the LMS_Sessions table. Again, the data won't be connected to the original LMS_Users table, as we want to store the CURRENT UserID regardless of future department (DeptID) changes.Can you hook me up with something? I thought I was on the right track with the code in my original posting but I got nothing... Not even an error message. :) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-12-06 : 17:47:22
|
| If it's a one-time thing, then no reason to wrap in a stored procedure. Just run it from Query Analyzer. Try this:UPDATE ltsSET DeptID = lu.DeptID FROM LMS_TestSessions ltsINNER JOIN LMS_Users luON lts.UserID = lu.UserIDWHERE lts.DeptID <> lu.DeptIDBut do you even need the WHERE clause? Is there other data in there that you don't want to update?Tara |
 |
|
|
MrRay
Starting Member
28 Posts |
Posted - 2004-12-06 : 19:05:17
|
quote: Originally posted by tduggan If it's a one-time thing, then no reason to wrap in a stored procedure. Just run it from Query Analyzer. Try this:UPDATE ltsSET DeptID = lu.DeptID FROM LMS_TestSessions ltsINNER JOIN LMS_Users luON lts.UserID = lu.UserIDWHERE lts.DeptID <> lu.DeptIDBut do you even need the WHERE clause? Is there other data in there that you don't want to update?Tara
Yeah, the where clause doesn't matter so much.So basically I had the right code, but the tables were inverted? Here's what I got out of your sample:CREATE PROCEDURE [dbo].[LMS_TestSessionDeptIDUpdate] ASUPDATE LMS_TestSessionsSET LMS_TestSessions.DeptID = LMS_Users.DeptID FROM LMS_TestSessionsINNER JOIN LMS_UsersON LMS_TestSessions.UserID = LMS_Users.UserIDWHERE LMS_TestSessions.DeptID <> LMS_Users.DeptIDGO |
 |
|
|
|
|
|
|
|