Tuesday, August 26, 2008

Create Sql server Function and call it as requred (2 input parameters)

CREATE FUNCTION [dbo].[ufIsUserHavingDependendency]
(
@UserId int,
@RoleId int
)
RETURNS int
AS

BEGIN

DECLARE @IsAssigned BIT
DECLARE @Count INT
DECLARE @mRoleId INT
SET @mRoleId=@RoleId
SET @Count = 0

--if role is Supervisor or admin

IF @mRoleId='2' OR @mRoleId='1'
BEGIN

--1.Has created or assigned Courses. (Supervisor)
SELECT @Count = COUNT(tblUserCreatedCourses.UserId) FROM tblUserCreatedCourses
WHERE tblUserCreatedCourses.UserId = @UserId AND @mRoleId<>1
IF(@Count>0)
return 1

--2. Has directly assigned Courses. (Student)
SELECT @Count = COUNT(tblCourse.CourseId) FROM tblCourse
WHERE tblCourse.SupervisorUserId = @UserId
IF(@Count>0)
return 1

-- SELECT @Count = COUNT(tblStudentCourse.StudentId) FROM tblStudentCourse
-- WHERE tblStudentCourse.StudentId=@UserId
-- IF(@Count>0)
-- return 1

--06. Has created ad hoc Question/s? (Supervisor)
SELECT @Count = COUNT(tblCourse.CourseId) FROM tblCourse
INNER JOIN tblQuestion
ON tblQuestion.CourseId= tblCourse.CourseId
WHERE tblCourse.SupervisorUserId = @UserId AND tblQuestion.CourseId <>'0'
IF(@Count>0)
return 1

--07. Has created Course based Question/s? (Supervisor)
SELECT @Count = COUNT(tblQuestion.QuestionId) FROM tblQuestion
WHERE tblQuestion.OwnerId = @UserId AND tblQuestion.CourseId ='0'
IF(@Count>0)
return 1

--09. Has non confirmed Course based Exam/s. (Supervisor)
SELECT @Count = COUNT(tblQuestionPaper.PaperId) FROM tblQuestionPaper
WHERE tblQuestionPaper.OwnerId = @UserId AND tblQuestionPaper.CourseId ='0'
IF(@Count>0)
return 1

--10
SELECT @Count = COUNT(tblCourse.CourseId) FROM tblCourse
INNER JOIN tblQuestionPaper
ON tblCourse.CourseId= tblQuestionPaper.CourseId
WHERE tblCourse.SupervisorUserId = @UserId AND tblQuestionPaper.CourseId <>'0'
IF(@Count>0)
return 1

--17 To 21
SELECT @Count = COUNT(tblUserExam.UserId) FROM dbo.tblUserExam
INNER JOIN dbo.tblQuestionPaper
ON tblQuestionPaper.PaperId=tblUserExam.PaperId
INNER JOIN dbo.tblCourse
ON tblCourse.CourseId=tblQuestionPaper.CourseId
WHERE tblCourse.SupervisorUserId =@UserId AND tblQuestionPaper.CourseId<>'0'
IF(@Count>0)
return 1

--Add hoc
SELECT @Count = COUNT(tblUserExam.UserId) FROM dbo.tblUserExam
INNER JOIN dbo.tblQuestionPaper
ON tblQuestionPaper.PaperId=tblUserExam.PaperId
WHERE dbo.tblQuestionPaper.OwnerId=@UserId AND tblQuestionPaper.CourseId='0'

IF(@Count>0)
return 1

--22
SELECT @Count = COUNT(dbo.tblVideoUser.UserId) FROM dbo.tblVideoUser
WHERE dbo.tblVideoUser.UserId=@UserId
IF(@Count>0)
return 1

END

ELSE IF @mRoleId='3'
BEGIN
--05
SELECT @Count = COUNT(dbo.tblCurriculumStudent.StudentId) FROM dbo.tblCurriculumStudent
WHERE tblCurriculumStudent.StudentId=@UserId
IF(@Count>0)
return 1

--23
SELECT @Count = COUNT(tblNotifications.NotificationID) FROM tblNotifications
INNER JOIN dbo.tblUserNotifications
ON tblNotifications.NotificationID=tblUserNotifications.NotificationID
WHERE dbo.tblUserNotifications.UserID=@UserId AND dbo.tblNotifications.NotificationType='1'--for alert
IF(@Count>0)
return 1

--26
SELECT @Count = COUNT(tblCourseRequest.StudentId) FROM tblCourseRequest
WHERE tblCourseRequest.StudentID=@UserId AND tblCourseRequest.ApprovedStatus='Pending'
IF(@Count>0)
return 1

--27
SELECT @Count = COUNT(tblExamRequest.StudentID) FROM tblExamRequest
WHERE tblExamRequest.StudentID=@UserId
IF(@Count>0)
return 1

--12
SELECT @Count = COUNT(tblUserExam.UserId) FROM tblUserExam
WHERE tblUserExam.UserId=@UserId
IF(@Count>0)
return 1

--13 check student is involved to a course
SELECT @Count = COUNT(tblStudentCourse.StudentId) FROM tblStudentCourse
WHERE tblStudentCourse.StudentId=@UserId
IF(@Count>0)
return 1
END

ELSE

BEGIN
return 0
END

return -3
END
//


//Calling the function sql stored procedure

SELECT tblUsers.Title,
tblUsers.UserID,
tblUsers.UserName,
tblUsers.FirstName,
tblUsers.LastName,
tblUnit.UnitName,
tblRole.RoleId,
tblRole.Description As RoleName,
tblGroups.GroupName,
tblUsers.LastLogDate,
tblUsers.IsActive,
tblUsers.IsSelfRegister,
dbo.ufIsUserHavingDependendency(tblUsers.UserID,tblUsers.RoleID) AS IsUserHavingDependendency,

No comments: