The following is my code. What I am trying to do is find all the students a teacher as assessed during a give time. Then find out which assessment was done the most recently. After that I will then be aggregating those results. I have never written any pl/sql or T-SQL... heck I don't even know what to call it!
The first sql command is doing what I want it to. I can only assume the cursor is working correctly.
Any help would be greatly appreciated.
Bryan
ALTER procedure Domain
@.UserID numeric,
@.StartDate datetime,
@.EndDate datetime
AS
-- Variable Decleration
DECLARE @.SessionID varchar(1000)
DECLARE @.EachSessionID numeric
--Cursor to find all Children that the teacher has assessed for the given time
DECLARE ChildID_cursor CURSOR
FOR
SELECT DISTINCT childID
FROM capsession
WHERE userid = @.UserID
AND sessiondate BETWEEN @.StartDate AND @.EndDate
-- looping through all Children to find there most recent assessment.
OPEN ChildID_cursor
DECLARE @.ChildID numeric
FETCH NEXT FROM ChildID_cursor INTO @.ChildID
WHILE (@.@.FETCH_STATUS <> -1)
BEGIN
IF (@.@.FETCH_STATUS <> -2)
BEGIN
DECLARE SessionID_cursor CURSOR
FOR
SELECT TOP 1 CAPSessionID
FROM CapSession
WHERE (ChildID = @.ChildID) AND (SessionDate BETWEEN @.StartDate AND @.EndDate)
ORDER BY SessionDate DESC
END
FETCH NEXT FROM ChildID_cursor INTO @.ChildID
END
CLOSE ChildID_cursor
OPEN SessionID_cursor
FETCH NEXT FROM SessionID_cursor into @.EachSessionID
SET @.SessionID = ''
WHILE (@.@.FETCH_STATUS <> -1)
BEGIN
IF (@.@.FETCH_STATUS <> -2)
BEGIN
SET @.SessionID = @.SessionID + @.EachSessionID+ ','
END
FETCH NEXT FROM ChildID_cursor INTO @.ChildID
END
CLOSE SessionID_cursor
RETURN @.EachSessionID
You can do what you need with a single SELECT statement. I believe this will cover it:
SELECT childID, MAX(SessionID) AS SessionID
FROM capsession
WHERE userid = @.UserID
AND sessiondate BETWEEN @.StartDate AND @.EndDate
GROUP BY childID
|||First of all that SQL worked perfectly Thanks! You really knocked that one out of the park. I have a couple questions for you. My understanding of T-SQL is that it should be used for procedural work. I mean if all I am going to do is return a simple result set why use it? (Just trying to understand the theory so I can be a better programmer) Second is there a good book you can recommend so I can learn the basics.
Thanks again for your rapid and expert advice.
Bryan|||I'm glad that helped you!
I had a typo in my post -- I meant "andnot RESORT to cursors. " By procedural code, I mean code thatdoes processing in a line-by-line fashion, typically using loops toiterate through a set of data. With T-SQL, you can perform tasksin one fell swoop, which is called a set-based approach - this wouldinclude, among others, SELECT, UPDATE, and INSERT commands.
Boy, I've learned most of what I know just by participating onlistservs and forums and migrating from Visual FoxPro to Access to SQLServer.. SQL Server 2000 Books Onlineis Microsoft's documentation for SQL Server and is an awesome freeresource -- I refer to it continually. Anyone doing SQL workshould have it. Several people I know and respect haverecommended this book:Rob Veiera's Professional SQL Server 2000 Programming. Another good resource is Ken Henderson'sThe Guru's Guide to Transact-SQL, although I don't think of this as a beginner's book.
No comments:
Post a Comment