Hi @ron barlow ,
After checking your statement, I found that there is not necessary to create a test table in your procedure.
You could refer below and check whether it is helpful to you:
CREATE PROCEDURE USP_PROCEDURE --your procedure name
@pClass VARCHAR(120)
AS
BEGIN
DECLARE @SQL NVARCHAR(MAX)
IF @pClass= 'ABC'
SET @SQL=' AND d.Fied1Z=1 '
ELSE IF @pClass= 'DEF'
SET @SQL=' AND ((d.FieldB=1) OR (d.FieldC=1))'
ELSE
SET @SQL='AND ((d.PreviousCompleteMonth=1) OR (d.Previous2ndCompleteMonth=1))'
SET @SQL = '
SELECT * FROM Student l
INNER JOIN dbo.StudentDetails f ON l.StudentKey=f.StudentKey
INNER JOIN [dbo].[StudentStatus] ps ON ps.StudentKey=f.StudentKey
INNER JOIN dbo.Date d ON d.DateKey=f.StartDateKey
INNER JOIN dbo.StudentAccount a ON a.AccountKey=f.AccountKey
WHERE a.StudentAccLevel= '+@SQL
EXECUTE sp_executesql @SQL
END
Then you could execute this procedure like below:
EXEC USP_PROCEDURE 'ABC'
EXEC USP_PROCEDURE 'DEF'
EXEC USP_PROCEDURE 'GGG'
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.