将存储过程的结果插入表中

时间:2013-10-04 16:23:03

标签: sql-server stored-procedures

我创建了以下SP,并希望将它返回的数据输出到同一数据库中的新表。一直试图使用“SELECT * INTO table FROM”示例,但没有运气。

执行此功能的最佳方法是什么?

我已将我创建的SP包含在内以供参考。

非常感谢!

USE [phoneview]
GO

/****** Object:  StoredProcedure [dbo].[querySkillsOverall]    Script Date: 10/4/2013 12:19:39 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Create date: 11/19/2012
-- Description: gets Skill info for Wallboard
-- =============================================
CREATE PROCEDURE [dbo].[querySkillsOverall] 
    -- Add the parameters for the stored procedure here
    @ChannelName varchar(100),
    @GroupName varchar(100)=NULL
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT 
    ROUND(CAST((SUM(tbl_SkillsLive.Abandoned)*100) as float)/NULLIF((SUM(tbl_SkillsLive.Received)),'0'),1) as 'Abandon %',
    ROUND(((SUM(CAST(tbl_SkillsLive.WithinSLA as float))*100)/NULLIF(SUM(CAST(tbl_SkillsLive.Received as float)),'0')),1) as 'Within SLA %',
        SUM(Received) as Received,
        SUM(Abandoned) as Abandoned,
        tbl_Channels.AbnExp,
        tbl_Channels.AbnMin,
        tbl_Channels.SLAExp,
        tbl_Channels.SLAMin,
        CASE WHEN SUM(tbl_SkillsLive.Handled) >=1 then (SUM(tbl_SkillsLive.ASA)/SUM(CASE WHEN ASA >0 THEN Handled ELSE NULL END)) Else NULL  END as 'AnsTime'
    FROM tbl_SkillsLive
    LEFT JOIN tbl_Skills on tbl_Skills.Skill_KeyID = tbl_SkillsLive.Skill_KeyID
    LEFT JOIN tbl_Skills_Channels_Link ON tbl_Skills_Channels_Link.Skill_KeyID = tbl_Skills.Skill_KeyID
    LEFT JOIN tbl_Channels on tbl_Channels.ChannelID = tbl_Skills_Channels_Link.ChannelID
    LEFT JOIN tbl_Groups on tbl_Groups.GroupID = tbl_Skills_Channels_Link.GroupID
    WHERE tbl_Channels.Name = @ChannelName
    AND (@GroupName is NULL OR tbl_Groups.Name = @GroupName)
    GROUP BY tbl_Channels.Name,
                tbl_Channels.AbnExp,
                tbl_Channels.AbnMin,
                tbl_Channels.SLAExp,
                tbl_Channels.SLAMin

END
GO

2 个答案:

答案 0 :(得分:0)

proc需要将表返回给调用脚本才能使select into正常工作。或者你可能只需要为你插入表格。

答案 1 :(得分:0)

你需要使用类似的东西来调用程序:

INSERT INTO #temptable
     SELECT * FROM dbo.querySkillsOverall(@ChannelName,@GroupName)