I am a beginner to SQL, and I am having trouble getting rid of repeated code. I have a condition "AccountId=20" that I use twice in a SQL query. The SQL looks something like:
DELETE FROM Table_A WHERE FunctionId IN (Select FunctionId FROM Table_B WHERE AccountId=20); DELETE FROM Table_B WHERE AccountId=20;
When I change the condition, I have to change both lines in the code.
I want to clean up the repeated code. I have tried something like:
DECLARE @condition datatypeForConditions = AccountId=20; DELETE FROM Table_A WHERE FunctionId IN (Select FunctionId FROM Table_B WHERE @condition); DELETE FROM Table_B WHERE @condition;
SQL does not seem to have a datatype for conditions, so I cannot declare this variable. Can I even set a variable for a condition? How can I avoid having to change the condition in two places?