SQL将列从一个过程插入另一个过程

时间:2010-11-09 00:25:22

标签: sql-server-2005 select exec procedure

我有一个返回许多列的存储过程并且使用起来非常麻烦,我创建了另一个将行转换为列的存储过程。如何将子过程中的列插入主过程?

我认为我想把子程序中的列放在我的代码中:

(select * from subprocedure where X= 1) as subquerycolumns

这是正确的吗?

这是我的表函数,但是我收到“列前缀'IPAM_RIPS_SKILL-SET_PARTICIPANT_RATING_HDR”与表名或别名不匹配...“错误,一切看起来不错,但有人可以帮助我。

CREATE FUNCTION PARTICIPANTRATINGSET
(@PARTICIPANT_ID INT)
RETURNS TABLE
AS
RETURN
    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 IPAM_RIPS_SKILL_SET_PARTICIPANT_RATING_HDR.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
GO

3 个答案:

答案 0 :(得分:1)

您不能以这种方式使用程序,您需要使用table functions在表格或视图等查询中使用结果。

答案 1 :(得分:0)

OPENROWSET进入临时表,您可以从中进行插入。

来自this link

use tempdb
go

select
    a.*
into
    MyTemp
from
    openrowset('SQLOLEDB','SERVER=(local);Trusted_Connection=yes;',
    '
    SET FMTONLY OFF;
    EXEC SP_HELPDB
    ') a

INSERT INTO MyTable (a, b, c)
select a, b, c from MyTemp

drop table MyTemp

答案 2 :(得分:0)

表函数不起作用并导致太多问题。我选择了一个视图,然后将jsut视为好的