Sql server 2005 我正在尝试根据存储过程的结果运行查询
首先,以下是sp_GetWorkQByUserName的代码:Sql server 2005 我正在尝试根据存储过程的结果运行查询,sql-server-2005,select,stored-procedures,distinct,execute,Sql Server 2005,Select,Stored Procedures,Distinct,Execute,首先,以下是sp_GetWorkQByUserName的代码: ALTER PROCEDURE [dbo].[sp_GetWorkQByUserName] ( @UserName varchar(50), @StartDate datetime, @EndDate datetime ) AS BEGIN SET NOCOUNT ON; SELECT DISTINCT SpotId FROM tblSpotCo
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
我知道我的selectdistinct
语句是错误的,但我这样写是为了帮助说明我在做什么。我想基于参数为@UserName
的sp_GetUserLocationCodes
的结果运行此存储过程
据我所知,我的问题在于如何调用sp\u GetUserLocationCodes
问题:如何基于
sp_GetUserLocationCodes
存储过程的结果对tblSpotCount.SpotId
运行SELECT DISTINCT
查询?您不能在查询中直接使用存储过程。但是,您可以将存储过程的结果插入到临时表中,并在查询中使用该临时表:
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