-1

Currently I have a query full of commented blocks of code that I activate/deactivate manually. My plan is to put everything in one stored procedure and have parameters to determine which commented blocks of code should run. Is there a way to do this?

Thanks!

double-beep
  • 5,031
  • 17
  • 33
  • 41
B Ruppert
  • 7
  • 3
  • There is a GOTO (depreciated) Take a peek at https://learn.microsoft.com/en-us/sql/t-sql/language-elements/goto-transact-sql?view=sql-server-ver15 – John Cappelletti May 17 '21 at 17:22
  • I may be wrong about the depreciated status. – John Cappelletti May 17 '21 at 17:32
  • I previously saw the docs, and then saw this: https://stackoverflow.com/questions/3046017/sql-goto-statement So I was wondering if there were other ways to go about this, but I guess I'll give it a go. My code is already poorly designed. – B Ruppert May 17 '21 at 17:32
  • Sounds like an opportunity to re-tool and improve ... the value-add is the fun part :) – John Cappelletti May 17 '21 at 17:39
  • 1
    Monday motivation! Cheers. – B Ruppert May 17 '21 at 17:42
  • You "have **a** query full of commented blocks of code", not multiple statements? It sounds like you are trying to enable/disable joins and filtering conditions. If you would show us a bit'o'code it would help us help you. – HABO May 18 '21 at 03:17

1 Answers1

1

IF is vastly preferable to GOTO. And you can generally avoid jumping around in the procedure or having lots of nested IF blocks with a pattern like this;

IF ...
BEGIN
  --DO SOMETHING
  RETURN;
END

IF ...
BEGIN
  --DO SOMETHING ELSE
  RETURN;
END

And it's the first step to refactoring the code blocks into separate single-responsibility stored procedures.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67