Thanks, I tried to use 'EXEC' before I posted but I guess I just don't understand its scope well enough. I still cannot figure out where to set the @sql variable in my sp and where to code 'EXEC @sql'. My sp is below and I'm sure it looks sloppy to the pro's out there, but I'm pretty much a novice...
CREATE PROCEDURE sr_Wards @gradYr varchar(4),@rotList varchar(50),@wkID INT AS
SET NOCOUNT ON
/* This cursor will be for students who are eligible for the given ward */
DECLARE cursRandStudents CURSOR FOR
SELECT DISTINCT s.int_ref_id,s.namelast,s.namefirst,numDuty=0,numBackup=0
FROM ((spSchedule sch JOIN rotStudent s ON sch.int_ref_id=s.int_ref_id)
JOIN spClassRotation cr ON cr.classRotID=sch.classRotID)
JOIN spRotation r ON r.RotationID=cr.RotID
WHERE s.classOf=@gradyr AND
Abbreviation IN (@rotList)
/* holding variables*/
DECLARE @int_ref_id varchar(11),
@namelast varchar(50),
@namefirst varchar(50),
@numDuty INT,
@numBackup INT
/*temp table for holding random list of students who are eligible for the given ward.*/
CREATE TABLE ##tmpRandStudent (
random numeric(18,18),
int_ref_id varchar(11),
namelast varchar(50),
namefirst varchar(50),
numDuty INT,
numBackup INT
)
OPEN cursRandStudents
FETCH NEXT FROM cursRandStudents INTO @int_ref_id,@namelast,@namefirst,@numDuty,@numBackup
WHILE (@@fetch_status <> -1) BEGIN
INSERT INTO ##tmpRandStudent (random, int_ref_id,namelast,namefirst,numDuty,numBackup) values (rand(), @int_ref_id,@namelast,@namefirst,@numDuty,@numBackup)
FETCH NEXT FROM cursRandStudents INTO @int_ref_id,@namelast,@namefirst,@numDuty,@numBackup
END
SELECT *
FROM ##tmpRandStudent
ORDER BY random
DROP TABLE ##tmpRandStudent
CLOSE cursRandStudents
DEALLOCATE cursRandStudents
GO