我不完全得到你想要的學生,讓這裏的是一些代碼,採用第一部分的護理。隨機分配一個調查給老師。也許你可以用它來弄清楚你應該如何解決剩下的問題。
-- Table with instructors
declare @Teacher table(TeacherID int, Name varchar(15))
insert into @Teacher values (1, 'Instructor 1')
insert into @Teacher values (2, 'Instructor 2')
insert into @Teacher values (3, 'Instructor 3')
insert into @Teacher values (4, 'Instructor 4')
insert into @Teacher values (5, 'Instructor 5')
-- Table with the four surveys
declare @Survey table(SurveyID int, SurveyName varchar(15))
insert into @Survey values (1, 'Survey 1')
insert into @Survey values (2, 'Survey 2')
insert into @Survey values (3, 'Survey 3')
insert into @Survey values (4, 'Survey 4')
-- Target table for Teacher and randomly selected Survey
declare @TeacherSurvey table(TeacherID int, SurveyID int)
insert into @TeacherSurvey (TeacherID, SurveyID)
select
T.TeacherID,
(select top 1 SurveyID
from @Survey
order by newid()) as SurveyID
from @Teacher as T
行中@TeacherSurvey
TeacherID SurveyID
----------- -----------
1 2
2 2
3 4
4 4
5 3
是否 「其他」 現有的表有surveyID列呢?如果沒有它,你將無法分辨哪些學生被分配給哪個教師進行哪項調查。你試過什麼了? – rsbarro 2011-04-13 14:10:56