在sql查询中获取没有添加列的表

时间:2016-07-19 12:09:13

标签: sql sql-server

我在sql server数据库中有一个Process表,如下所示:

Process Table

如果Process中存在Rolename,我需要选择RoleTable,如下所示:

 select ID,WorkflowXML 
    from 
    (
    select m.c.value('@RoleName','nvarchar(max)') as Rolename,p.*
    from Process as p
    outer apply p.WorkflowXML.nodes('/workflow/Tasks/Task[@type="start"]/TaskUsers/TaskUser') as m(c)
    ) as Pro where Pro.Rolename in
    (SELECT * from RoleTable)

在此选择中Rolename添加到Process。我只需要Process表。我可以添加Process个字段(ID,WorkflowXML)进行查询。如果我更改Process表格设计,我应该编辑查询。这不是很好。

有没有办法在查询中选择Process表而不添加像这样的Process字段?

select Pro
        from 
        (
        select m.c.value('@RoleName','nvarchar(max)') as Rolename,p.*
        from Process as p
        outer apply p.WorkflowXML.nodes('/workflow/Tasks/Task[@type="start"]/TaskUsers/TaskUser') as m(c)
        ) as Pro where Pro.Rolename in
        (SELECT * from RoleTable)

感谢。

1 个答案:

答案 0 :(得分:1)

您可以加入此表格,如:

select p.*
from Process as p
outer apply p.WorkflowXML.nodes('/Tasks/Task[@type="start"]/TaskUsers/TaskUser') as  m(c)
INNER JOIN RoleTable r 
    ON r.Rolename = m.c.value('@RoleName','varchar(max)')