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

发布于 2024-10-01 05:30:11 字数 3112 浏览 6 评论 0原文

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

我认为如果我想将子过程中的列放入我的代码中:

(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

I have a stored procedure that returns many columns and is very cumbersome to work with, I created another stored procedure that converts rows to columns. How do I insert the columns from the sub procedure into the main procedure?

I am think that were I want to put the columns from the sub procedure I code it like:

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

is this correct?

Here is my Table Function, however I am receiving a "The column prefix 'IPAM_RIPS_SKILL-SET_PARTICIPANT_RATING_HDR" does not match with a table name or alias..." error, Everything looks fine to me though, can someone help me.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-10-08 05:30:11

您不能以这种方式使用过程,您需要使用表函数用于在表或视图等查询中使用结果。

You you can't use procedures in that way, you need to use table functions for using the results in queries like a table or view.

一笔一画续写前缘 2024-10-08 05:30:11

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

来自此链接

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

OPENROWSET into a temp table, from which you can do your insert.

From 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
生生不灭 2024-10-08 05:30:11

表功能不起作用并导致了太多问题。我选择了一个风景,效果却同样好

The table function did not work and was causing too many issue. I went with a view instead which worked jsut as good

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文