1

Hi I have created a view in SQL-SERVER and I would also like to be able to add a column to the returned result of this view.

Here is my code:

 ALTER VIEW [eConnect].[GetStudentTeachers]
AS
    select 
        srmitv.StudentId as StudentId, 
        srmitv.TeacherAlias as Alias, 
        srmitv.TeacherAvatarFileName as AvatarFileName, 
        srmitv.TeacherMessageUserId as MessageUserId, 
        srmitv.TeacherUserName as UserName,
        sp.Sex as Gender,
    from 
        StudentRegularModuleInstanceTeachersView srmitv 
        inner join Sync_Persons sp on srmitv.TeacherId = sp.TeacherID and sp.Deletion_Date is null

    union 
    select 
        somirtv.StudentId as StudentId, 
        somirtv.TeacherAlias as Alias, 
        somirtv.TeacherAvatarFileName as AvatarFileName, 
        somirtv.TeacherMessageUserId as MessageUserId, 
        somirtv.TeacherUserName as UserName,
        sp.Sex as Gender
    from 
        dbo.StudentOlcModuleInstanceRelatedTeachersView somirtv
        inner join Sync_Persons sp on somirtv.TeacherId = sp.TeacherID and sp.Deletion_Date is null
GO

Now I would like to add another column here RoleId that always has the value 1.How can I achieve this?

2 Answers 2

3

Just add this to the select: 1 as RoleId

ALTER VIEW [eConnect].[GetStudentTeachers]
AS
    select 
        srmitv.StudentId as StudentId, 
        srmitv.TeacherAlias as Alias, 
        srmitv.TeacherAvatarFileName as AvatarFileName, 
        srmitv.TeacherMessageUserId as MessageUserId, 
        srmitv.TeacherUserName as UserName,
        sp.Sex as Gender,
        1 as RoleId 

    from 
        StudentRegularModuleInstanceTeachersView srmitv 
        inner join Sync_Persons sp on srmitv.TeacherId = sp.TeacherID and sp.Deletion_Date is null

    union 
    select 
        somirtv.StudentId as StudentId, 
        somirtv.TeacherAlias as Alias, 
        somirtv.TeacherAvatarFileName as AvatarFileName, 
        somirtv.TeacherMessageUserId as MessageUserId, 
        somirtv.TeacherUserName as UserName,
        sp.Sex as Gender,
        1 as RoleId 
    from 
        dbo.StudentOlcModuleInstanceRelatedTeachersView somirtv
        inner join Sync_Persons sp on somirtv.TeacherId = sp.TeacherID and sp.Deletion_Date is null
GO
0
0

Not sure if this is what you want

SELECT 1 AS RoleID, * FROM [eConnect].[GetStudentTeachers]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.