将多个数据分配给 sql 服务器存储过程中的一个变量

Assign multiple data to a variable in sql server stored procedure

我想为存储过程中 SELECT 语句中的变量分配多个值。 代码是这样的

DECLARE @ProjectExecutionPlanId INT=NULL

SELECT @ProjectExecutionPlanId = (SELECT [ID] FROM [dbo].[ProjectExecutionPlan] 
                                  WHERE ProjectDetailID=@PID)

@PID 在存储过程的输入中。

SELECT声明returns多个值。所以我收到错误。

此答案基于您的评论:

I want to delete multiple rows from the ProjectExecutionPlanExecution table which is dependent on ProjectExecutionPlan table.There are multiple plans in ProjectExecutionPlan table. So I will get multiple IDs

Delete From ProjectExecutionPlanExecution 
Where ProjectExecutionPlanId In (SELECT [ID]
                                 FROM [dbo].[ProjectExecutionPlan] 
                                 WHERE ProjectDetailID=@PID)

或者

Delete pe From ProjectExecutionPlanExecution pe
Join ProjectExecutionPlan p On pe.ProjectExecutionPlanID = p.ID
WHERE p.ProjectDetailID=@PID