SQL 将列从一个过程插入到另一个过程
我有一个返回许多列的存储过程,使用起来非常麻烦,我创建了另一个将行转换为列的存储过程。如何将子过程中的列插入到主过程中?
我认为如果我想将子过程中的列放入我的代码中:
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能以这种方式使用过程,您需要使用表函数用于在表或视图等查询中使用结果。
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.
OPENROWSET 到临时表中,您可以从中进行插入。
来自此链接:
OPENROWSET into a temp table, from which you can do your insert.
From this link:
表功能不起作用并导致了太多问题。我选择了一个风景,效果却同样好
The table function did not work and was causing too many issue. I went with a view instead which worked jsut as good