首先,这是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.SpotId
上运行sp_GetUserLocationCodes
查询?
答案 0 :(得分: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