2013-07-12 129 views
1

嗨我已經在SQL SERVER中創建了一個視圖,我也想能夠添加一列到這個視圖的返回結果。添加一列到結果

這裏是我的代碼:

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 

現在我想在此補充另一列是角色ID總是具有價值1.How我能做到這一點?

回答

3

只需添加這選擇: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

因爲它是一個聯盟,角色ID必須在兩個選擇 –

+0

尼斯之一,補充說。 – hutchonoid

0

不知道這是否是你想要的

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