更改存储过程以包括来自另一个存储过程/查询的结果

时间:2011-04-06 21:22:16

标签: sql tsql

我有这个存储过程,它返回给定params

的客户端详细信息列表
PROCEDURE [dbo].[vExport_GetClientDetailsBySchemeAndTeam]
            @startdate DATETIME = null,
            @enddate DATETIME = NULL,
            @scheme INT,
            @teamID INT
 AS
/*returns a  list of current client details between two dates */


SELECT 
    Forename,
    Surname,
    Address1,
    Address2,
    Town,
    County,
    Postcode,
    [Status],
    Telephone,
    EmergencyTelephone
    --exec vExport_GetClientDetailsBySchemeAndTeam '2010-04-06 12:00:00', '2011-04-06 12:00:00', '3', '152'
FROM
    vClients
WHERE
    vClients.ClientID in
        (SELECT DISTINCT
            E.ClientID 
            FROM vEvents E
            INNER JOIN vClients C on E.ClientID = C.ClientID
            WHERE E.EventDate between @startdate and @enddate
            --AND C.Status = 0
            AND e.SchemeID=@scheme
            AND e.TeamID = @teamID)

但是我现在需要在select(last eventdate)中包含另一个SP的结果。基本上看起来像这样:

SELECT TOP 1 * from vEvents  E where E.ClientID =  @ClientID
order by e.EventDate desc

如何使用第二个查询返回第一个select语句中所有客户端的最后一个事件日期?

滞留!!

5 个答案:

答案 0 :(得分:1)

PROCEDURE [dbo].[vExport_GetClientDetailsBySchemeAndTeam]
            @startdate DATETIME = null,
            @enddate DATETIME = NULL,
            @scheme INT,
            @teamID INT
 AS
/*returns a  list of current client details between two dates */


SELECT 
    Forename,
    Surname,
    Address1,
    Address2,
    Town,
    County,
    Postcode,
    [Status],
    Telephone,
    EmergencyTelephone,
    (SELECT top 1 e.EventDate from vEvents  E where E.ClientID =  vClients.ClientID order by e.EventDate desc)
    --exec vExport_GetClientDetailsBySchemeAndTeam '2010-04-06 12:00:00', '2011-04-06 12:00:00', '3', '152'
FROM
    vClients
WHERE
    vClients.ClientID in
        (SELECT DISTINCT
            E.ClientID 
            FROM vEvents E
            INNER JOIN vClients C on E.ClientID = C.ClientID
            WHERE E.EventDate between @startdate and @enddate
            --AND C.Status = 0
            AND e.SchemeID=@scheme
            AND e.TeamID = @teamID)

答案 1 :(得分:0)

这是你的朋友:

create table #sp_output
(
  col_1 ... ,
  ...
  col_n ... ,
)

INSERT #sp_output EXECUTE <your-stored-procedure-of-choice>

需要注意的是,存储过程必须返回单个结果集,并且表模式必须与该结果集完全匹配。因此,如果调用的存储过程发生更改,则会破坏存储过程,直到更新表定义以匹配。

答案 2 :(得分:0)

将其组合为针对派生表的连接:

SELECT 
    Forename,
    Surname,
    Address1,
    Address2,
    Town,
    County,
    Postcode,
    [Status],
    Telephone,
    EmergencyTelephone,
    E.MostRecentEventDate
FROM
    vClients C INNER JOIN (
        SELECT
            ClientID,
            MAX(EventDate) AS [MostRecentEventDate]
        FROM vEvents
        WHERE EventDate BETWEEN @startdate AND @enddate
            AND SchemeID=@scheme
            AND TeamID = @teamID
        GROUP BY ClientID
    ) E ON C.ClientID = E.ClientID

将临时表用于这种简单的操作是不必要的,并且完全浪费了数据库资源。

答案 3 :(得分:0)

我看到两个解决方案:

  1. 使用表函数代替过程(自SQL Server 2005起可用)



  2. 使用Table变量(DECLARE @temp TABLE(...))使用临时表,该变量与其他存储过程的结果相同。然后使用INSERT + EXECUTE语句将其他存储过程的结果存储到临时表中。
    INSERT @temp EXECUTE OtherStoredProc
    然后你得到表@temp
  3. 中其他存储过程的结果

答案 4 :(得分:-1)

您的问题的字面答案是创建一个全局临时表,并逐步填充该表。

例如:

    PROCEDURE [dbo].[vExport_GetClientDetailsBySchemeAndTeam] @startdate DATETIME = null, @enddate DATETIME = NULL, @scheme INT, @teamID INT AS /*returns a list of current client details between two dates */
    create table ##results(//columns you want to return)
insert into ##results (columns)

    SELECT Forename, Surname, Address1, Address2, Town, County, Postcode, [Status], Telephone, EmergencyTelephone --exec vExport_GetClientDetailsBySchemeAndTeam '2010-04-06 12:00:00', '2011-04-06 12:00:00', '3', '152' FROM vClients WHERE vClients.ClientID in (SELECT DISTINCT E.ClientID FROM vEvents E INNER JOIN vClients C on E.ClientID = C.ClientID WHERE E.EventDate between @startdate and @enddate --AND C.Status = 0 AND e.SchemeID=@scheme AND e.TeamID = @teamID)

update results
set (columns from you events table)
from ##results r, 
vEvents e
where e.clientid = r.clientid

select * from ##results

但是,我认为你也可以通过传统联接的礼物实现你想要的。值得一看,您是否可以从主查询中加入事件表...