2012-07-17 30 views
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

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

+0

是标量函数吗? – 2012-07-17 17:52:33

回答

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