如何在sql server

时间:2016-09-27 06:40:26

标签: sql-server

我有一个返回表的select语句。我想选择一个表值函数作为select语句的一部分,它也返回一个表..如何做到这一点。

这是我的选择代码,它返回一个表

SELECT 
    dbo.TC_User.JobTitle, dbo.TC_User.UserID, 
    dbo.TC_User.LocalID, dbo.TC_User.NokiaID, dbo.TC_User.NameCN,
    dbo.TC_User.职阶 as EmployeeGroup, 
    dbo.TC_User2.LockDateID, dbo.TC_User.StartDate, 
    dbo.TC_User.EndDate, dbo.TC_User.StartDateN, 
    dbo.TC_User.聘用前工号, dbo.TC_User.NoCheckFlag, 
    dbo.GetGroupPath2('465') AS Path  
FROM 
    dbo.TC_User 
INNER JOIN 
    dbo.TC_User2 ON dbo.TC_User.UserID = dbo.TC_User2.UserID 
WHERE 
    (dbo.TC_User.UserID IN (SELECT UserID 
                            FROM dbo.TC_User 
                            WHERE (GroupID IN (SELECT GroupID 
                                               FROM dbo.VSGetSubGroupTab(10, 1) AS VSGetSubGroupTab)))) 

这就是我想要的

SELECT 
    dbo.TC_User.JobTitle, dbo.TC_User.UserID,
    dbo.GetGroupPath2Clone('465') as grouppath,
    dbo.TC_User.LocalID, dbo.TC_User.NokiaID, dbo.TC_User.NameCN, 
    dbo.TC_User.职阶 as EmployeeGroup, 
    dbo.TC_User2.LockDateID, dbo.TC_User.StartDate, 
    dbo.TC_User.EndDate, dbo.TC_User.StartDateN, 
    dbo.TC_User.聘用前工号, dbo.TC_User.NoCheckFlag, 
    dbo.GetGroupPath2('465') AS Path  
FROM 
    dbo.TC_User 
INNER JOIN 
    dbo.TC_User2 ON dbo.TC_User.UserID = dbo.TC_User2.UserID 
WHERE 
    (dbo.TC_User.UserID IN (SELECT UserID 
                            FROM dbo.TC_User 
                            WHERE (GroupID IN (SELECT GroupID 
                                               FROM dbo.VSGetSubGroupTab(10, 1) AS VSGetSubGroupTab)))) 

dbo.GetGroupPath2Clone('465') as grouppath,是我想要调用的表值函数。怎么做?

1 个答案:

答案 0 :(得分:0)

dbo.GetGroupPath2看起来像一个标量函数。但是,如果它是表函数,则需要将其放在连接列表中。

SELECT 
dbo.TC_User.JobTitle, dbo.TC_User.UserID, 
dbo.TC_User.LocalID, dbo.TC_User.NokiaID, dbo.TC_User.NameCN,
dbo.TC_User.职阶 as EmployeeGroup, 
dbo.TC_User2.LockDateID, dbo.TC_User.StartDate, 
dbo.TC_User.EndDate, dbo.TC_User.StartDateN, 
dbo.TC_User.聘用前工号, dbo.TC_User.NoCheckFlag, 
 tf.yourfield1, tf.yourfield2, ...etc. 
FROM 
dbo.TC_User 
join dbo.GetGroupPath2('465') AS tf on tf.yourkey=somekeys
INNER JOIN 
dbo.TC_User2 ON dbo.TC_User.UserID = dbo.TC_User2.UserID 
WHERE 
(dbo.TC_User.UserID IN (SELECT UserID 
                        FROM dbo.TC_User 
                        WHERE (GroupID IN (SELECT GroupID 
                                           FROM dbo.VSGetSubGroupTab(10, 1) AS VSGetSubGroupTab)))) 

创建表函数:

CREATE FUNCTION [dbo].GetGroupPath2 
(
    @Parm varchar(10)
)
Returns Table 
As
Return (
    Select yourfield1, yourfield2
   From   yourTable
   where [yourKey] = @Parm
)