0

首先,这里是 sp_GetWorkQByUserName 的代码:

    ALTER PROCEDURE [dbo].[sp_GetWorkQByUserName]
    ( @UserName varchar(50),
      @StartDate datetime,
      @EndDate datetime )
    AS
    BEGIN
        SET NOCOUNT ON;
        SELECT DISTINCT SpotId FROM tblSpotCount WHERE StoreNum = EXECUTE sp_GetUserLocationCodes(@UserName)
        ORDER BY SpotDt ASC 
    END

我知道我的SELECT DISTINCT陈述是错误的,但我这样写是为了帮助展示我正在尝试做的事情。我想根据sp_GetUserLocationCodes带有参数的结果运行这个存储过程@UserName

据我所知,我的问题在于我如何打电话sp_GetUserLocationCodes

问题:如何根据存储过程的结果运行SELECT DISTINCT查询?tblSpotCount.SpotIdsp_GetUserLocationCodes

4

1 回答 1

1

您不能直接在查询中使用存储过程。但是,您可以将存储过程的结果插入到临时表中并在查询中使用它:

CREATE TABLE #storeLocations
(
    -- appropriate column names and data types go here
)

INSERT INTO #storeLocations (put column list here)
EXECUTE sp_GetUserLocationCodes(@UserName)

SELECT DISTINCT SpotId
FROM tblSpotCount
WHERE EXISTS (SELECT 1
              FROM #storeLocations
              WHERE #storeLocations.StoreNum = tblSpotCount.StoreNum)
ORDER BY SpotDt ASC

DROP TABLE #storeLocations
于 2012-07-17T15:28:45.163 回答