0

I want to assign multiple values to a variable from a SELECT statement in a stored procedure. The code goes like this

DECLARE @ProjectExecutionPlanId INT=NULL

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

@PID in the input to the stored procedure.

The SELECT statement returns multiple values. So I am getting error.

3
  • which of those multiple values do you want to set in @ProjectExecutionPlanId Commented Apr 16, 2015 at 10:53
  • What is the error? Perhaps you need to add TOP 1 to the inner select, or try and understand why you are getting multiple values. Commented Apr 16, 2015 at 10:58
  • I can't add TOP 1.I need all the IDs from ProjectExecutionPlan table Commented Apr 16, 2015 at 11:01

1 Answer 1

1

This answer is based on your comment:

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)

Or

Delete pe From ProjectExecutionPlanExecution pe
Join ProjectExecutionPlan p On pe.ProjectExecutionPlanID = p.ID
WHERE p.ProjectDetailID=@PID
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.