Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is part of a store procedure. 

	--select * from @MyTable;

	declare @seed int = (select Answer from @MyTable where IsSeed = 1);
	declare @drawDown int = (select sum(Answer) from @MyTable where IsSeed = 0);
	declare @ansToCheck nvarchar(max) = (select ans.answer from Answers ans where ans.ProjectID = @projectId and 	
										ans.QuestionID in (select ID from Questions where (ParentID = @parentQuestionId or ID = @parentQuestionId) and Day = @providedAnsDay));

	select 
		@projectId as ProjectId, 
		isnull(@parentQuestionId, -1) as ParentQuestionId, 
		@providedQuestionId as ProvidedQuestionId,
		isnull(@seed, 0) as Seed, 
		isnull(@drawDown, 0) as Expended, 
		isnull(@seed, 0) - isnull(@drawDown, 0) as Remaining,
		@ansToCheck as AnsToCheck;

END


I need to run delete statement if 'Remaining' is less than or equals to 0 before store procedure return. How do i do it ? 


What I have tried:

I know how to use if and else block but It will not let me put in select statement.

How do I achieve this.
Posted
Updated 2-Feb-18 3:47am
Comments
F-ES Sitecore 2-Feb-18 9:32am    
You should avoid putting business logic inside stored procedures. The code that calls this procedure should decide if something needs deleting and if it does it can instigate the delete statement.
istudent 2-Feb-18 10:04am    
I think about it sir and try not to do this from store procedure. It is due to implementation problem. This is something say, if there is some data for Monday Filed in (page 1) and Tuesday filed in (page2). But someone in future decided to edit Data for Monday to 0 then Tuesday Field should not be shown up in Page 2.
RedDk 2-Feb-18 14:23pm    
This looks like pseudo code. Clean it up to show TSQL and we're one step further down the path to figuring out what to do next.

1 solution

SQL
SELECT
    @projectId as ProjectId, 
    isnull(@parentQuestionId, -1) as ParentQuestionId, 
    @providedQuestionId as ProvidedQuestionId,
    isnull(@seed, 0) as Seed, 
    isnull(@drawDown, 0) as Expended, 
    isnull(@seed, 0) - isnull(@drawDown, 0) as Remaining,
    @ansToCheck as AnsToCheck;

If isnull(@seed, 0) - isnull(@drawDown, 0) <= 0
BEGIN
    ...
END;
 
Share this answer
 
Comments
Maciej Los 4-Feb-18 7:38am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900