| Author |
Topic |
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 10:15:04
|
| I have 3 tables. one is a User table, one is a Station table and the last is a UserStation table.for every user I need to add a default station in the userStation table. I am trying to piece the querry together and think i am getting something like the followingThe station will be retrieved by something like... where stationID in(select stationID from station where stationName = '053')The update will be something like... UPDATE UserStation SET UserID = (?not sure?), StationID = (?not sure?)How can i get all the UserID? |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-07-10 : 10:17:20
|
| Can you provide some sample data and expected output. |
 |
|
|
znwpta
Starting Member
12 Posts |
Posted - 2009-07-10 : 10:23:55
|
| I believe you can do something like thisINSERT INTO TABLE1 ([Field1], [Field2]) SELECT [TB2FIELDX], [TB2FIELDY] FROM TABLE 2 WHERE TABLE2ID=XI never tried with 2 tables, but I think if you tweak it it should work.Hope it helps. |
 |
|
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 10:24:29
|
| user1 UserID = 670, then other user info.User2 userID = 540, Station would have StationID = 1 (autoincrement) and StationName = '053'then UserStation(row1) would have UserStationID =1 (autoincrement), UserID = 670, StationId = '1'then UserStation(row2) would have UserStationID =2, UserID = 540, StationID = '1so on and on...I need this done for every user in the user table and i won't know the stationID in advance only the StationName of '053' in advance |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-07-10 : 10:28:41
|
| How are the User table and Station table related? Are there common fields to join these two tableS? |
 |
|
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 10:34:27
|
| Only through the join table called Userstation. UserStation looks like this...UserStationID | UserID | StationID1 | 670 | 12 | 540 | 1 |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-07-10 : 10:43:12
|
| I'm not sure I'm understanding this....If the Userstation table already contains the data for UserID and StationID, what should we be updating in that table? |
 |
|
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 10:53:32
|
| this is a new requirement to add a default station value to every user. So for every existing user I need to add a default stationID. The default stationName is '053'in code, I would get the default StationID by select * from Station where StationName = '053'. assign the reslut to a variable, then I would get all users and loop over all users to create a new UserStation row that would contain the new UserStationID, userID, StationID. but i dont know how to do that as a sql script. |
 |
|
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 11:07:10
|
| OPPS just realized i might have caused confusion in my first post by saying i would do an UPDATE.I meant I would INSERT a new record into UserStation.Sorry for any confusion. |
 |
|
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 11:10:21
|
| I am thinking this might work...DECLARE @StationID intSELECT @StationID = StationID FROM Station WHERE StationNumber ='053'DECLARE @UserID intDECLARE curUser CURSOR LOCAL FOR SELECT UserID FROM User OPEN curUser FETCH curUser INTO @UserID WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO UserStation (UserID, StationID) values (@UserID, @StationID) FETCH curUser INTO @UserID END CLOSE curUser DEALLOCATE curUser |
 |
|
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 11:27:27
|
| Yes, that worked. Thanks all for helping me work out the answer. |
 |
|
|
lance1524
Starting Member
11 Posts |
Posted - 2009-07-10 : 12:44:43
|
| Just incase anyone else needs to do something like this, here is my final result...DECLARE @StationID int--if the default 053 is not in the station table then add itIF(NOT EXISTS(SELECT StationID FROM Station WHERE StationNumber = '053')) INSERT INTO Station (StationNumber) VALUES ('053')-- Get the default stationIDSELECT @StationID = StationID FROM Station WHERE StationNumber ='053'DECLARE @UserID int--loop over all Users DECLARE curUser CURSOR LOCAL FOR SELECT UserID FROM User OPEN curUser --get first user FETCH curUser INTO @UserID WHILE @@FETCH_STATUS = 0 BEGIN --if the user already has the 053 StationNumber get the next user IF(EXISTS(SELECT * FROM User s JOIN UserStation u on u.UserID = @UserID WHERE u.StationID = @StationID)) FETCH curUser INTO @UserID --insert the default value into the UserStation table INSERT INTO UserStation (UserID, StationID) VALUES (@UserID, @StationID) --get next user FETCH curUser INTO @UserID END CLOSE curUser DEALLOCATE curUser |
 |
|
|
|