从SELECT存储过程访问SQL表函数列

时间:2010-11-10 19:30:11

标签: sql sql-server sql-server-2005 tsql stored-procedures

我有一个非常大的SELECT存储过程,它有多个返回列和多个INNER JOINS和LEFT JOINS。我还有一个包含10列的表函数,表函数的结果集基于用户ID。

我的问题是,如何将表函数合并到主SELECT存储过程中,并能够根据用户ID从表函数返回列。

主要存储程序:

SELECT     
      dbo.WebPersonalInfo.UserID, 
      dbo.WebPersonalInfo.FirstName, 
      dbo.WebPersonalInfo.MiddleInitial, 
      dbo.WebPersonalInfo.LastName,
      dbo.WebProgramParticipants.ParticipantID, 
      dbo.WebProgramParticipants.ProgramCode, 
FROM  dbo.WebProgramParticipants INNER JOIN
      dbo.WebPersonalInfo ON dbo.WebProgramParticipants.UserID = dbo.WebPersonalInfo.UserID 
WHERE     (dbo.WebProgramParticipants.ProgramCode = @programcode) 
ORDER BY dbo.WebPersonalInfo.LastName, dbo.WebPersonalInfo.FirstName

表功能:

SELECT 
        ST1.SKILL_SET_ID AS SET1, SS1.SET_NAME AS NAME1, ST1.SKILL_SET_RATING AS R1,
        ST2.SKILL_SET_ID AS SET2, SS2.SET_NAME AS NAME2, ST2.SKILL_SET_RATING AS R2,
        ST3.SKILL_SET_ID AS SET3, SS3.SET_NAME AS NAME3, ST3.SKILL_SET_RATING AS R3,
        ST4.SKILL_SET_ID AS SET4, SS4.SET_NAME AS NAME4, ST4.SKILL_SET_RATING AS R4,
        ST5.SKILL_SET_ID AS SET5, ST5.SKILL_SET_OPTIONAL AS NAME5, ST5.SKILL_SET_RATING AS R5,
        ST6.SKILL_SET_ID AS SET6, ST6.SKILL_SET_OPTIONAL AS NAME6, ST6.SKILL_SET_RATING AS R6,
        ST7.SKILL_SET_ID AS SET7, ST7.SKILL_SET_OPTIONAL AS NAME7, ST7.SKILL_SET_RATING AS R7,
        CM.COMMENTS
    FROM IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_HDR HT
    LEFT JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_DTL ST1
        ON HT.ID = ST1.HDR_ID and st1.SKILL_SET_ID = 1
    INNER JOIN IPAM_RIPS_SKILL_SETS SS1
        ON ST1.SKILL_SET_ID = SS1.ID
    LEFT JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_DTL ST2
        ON HT.ID = ST2.HDR_ID and st2.SKILL_SET_ID = 2
    INNER JOIN IPAM_RIPS_SKILL_SETS SS2
        ON ST2.SKILL_SET_ID = SS2.ID
    LEFT JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_DTL ST3
        ON HT.ID = ST3.HDR_ID and st3.SKILL_SET_ID = 3
    INNER JOIN IPAM_RIPS_SKILL_SETS SS3
        ON ST3.SKILL_SET_ID = SS3.ID
    LEFT JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_DTL ST4
        ON HT.ID = ST4.HDR_ID and st4.SKILL_SET_ID = 4
    INNER JOIN IPAM_RIPS_SKILL_SETS SS4
        ON ST4.SKILL_SET_ID = SS4.ID
    LEFT JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_DTL ST5
        ON HT.ID = ST5.HDR_ID and st5.SKILL_SET_ID = 5
    INNER JOIN IPAM_RIPS_SKILL_SETS SS5
        ON ST5.SKILL_SET_ID = SS5.ID
    LEFT JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_DTL ST6
        ON HT.ID = ST6.HDR_ID and st6.SKILL_SET_ID = 10
    INNER JOIN IPAM_RIPS_SKILL_SETS SS6
        ON ST6.SKILL_SET_ID = SS6.ID
    LEFT JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_DTL ST7
        ON HT.ID = ST7.HDR_ID and st7.SKILL_SET_ID = 11
    INNER JOIN IPAM_RIPS_SKILL_SETS SS7
        ON ST7.SKILL_SET_ID = SS7.ID
    INNER JOIN IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_COMMENTS CM
        ON HT.ID = CM.HDR_ID
    WHERE HT.PARTICIPANT_ID = @PARTICIPANT_ID
    GROUP BY PARTICIPANT_ID, ST1.SKILL_SET_ID, SS1.SET_NAME, ST1.SKILL_SET_RATING, ST2.SKILL_SET_ID, SS2.SET_NAME, ST2.SKILL_SET_RATING, ST3.SKILL_SET_ID, SS3.SET_NAME, ST3.SKILL_SET_RATING, ST4.SKILL_SET_ID, SS4.SET_NAME, ST4.SKILL_SET_RATING, ST5.SKILL_SET_ID, ST5.SKILL_SET_OPTIONAL, ST5.SKILL_SET_RATING, ST6.SKILL_SET_ID, ST6.SKILL_SET_OPTIONAL, ST6.SKILL_SET_RATING, ST7.SKILL_SET_ID, ST7.SKILL_SET_OPTIONAL, ST7.SKILL_SET_RATING, CM.COMMENTS

4 个答案:

答案 0 :(得分:2)

您可以使用outer applyinner apply来使用常规功能表:

select  *
from    users u
outer apply
        dbo.fn_YourFunction(u.id) yf

这将返回函数中的所有列。

答案 1 :(得分:0)

确切地知道你在问什么,但是稍微阅读一下......

您可以加入表值函数:

SELECT *
FROM existing tables and joins
LEFT JOIN tvf(@userid /* param to SP */)
    ON whatever

您还可以将表值函数应用于行:

SELECT *
FROM existing tables and joins
OUTER APPLY tvf(userid /* column from above joins */)

答案 2 :(得分:0)

您可以使用SELECT * FROM yourfunction()从sql函数中检索数据。所以,也许您可​​以在JOINS上使用UserID上的{{1}}和您的函数(),如上所示。

您需要更具体地获得更好的答案。

答案 3 :(得分:0)

Andomar的回答是正确的,但是......你考虑过只使用VIEW吗?更简单,几乎肯定更快。