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

现在我想在这里添加另一列 RoleId 始终具有值 1。我怎样才能实现这一点?

4

2 回答 2

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
于 2013-07-12T10:37:24.537 回答
0

不确定这是否是您想要的

SELECT 1 AS RoleID, * FROM [eConnect].[GetStudentTeachers]
于 2013-07-12T10:38:18.727 回答