I’m trying to optimize a stored procedure and cut down any redundant code. Right now there's a lot code that is duplicated based on the @schema
parameter. @Schema
can be the test environment or the prod environment.
So very simple example:
IF @schema = ’test’ BEGIN
SELECT*FROM test.dbo.test_results
END ELSE IF @schema = ‘prod’ BEGIN
SELECT*FROM final.dbo.final_results END;
See, same code, the only difference is the table name. Except the code on my project has hundreds of duplicated lines of code with the only difference being the table name.
Therefore, can I set a parameter as @tableName
where I can create the following case statement?:
IF @schema = ‘test’ BEGIN
SET @tableName = ‘test.dbo.test_results’
END ELSE IF @schema = ‘prod’ BEGIN
SET @tableName = ‘final.dbo.final_results’
Then have the following code:
SELECT*FROM @tableName
If not, then any other recommendations?