SQL Prompt Fixes Missing Begin End in Procs

SQL Prompt is a fantastic coding aid, but it does more than format your code and provide intellisense. Over time, the team has enhanced SQL Prompt to also guide you along and fix some bad code that your team might write out of habit.

SQL Prompt 10.1 released recently, and one of the recent fixes is for an issue that we’ve denoted as ST003. This is an issue where a procedure is not enclosed in a Begin  End. Here’s an example.

I started writing this proc, which looks normal. I’m not sure what isn’t being qualified with the red squiggles, as the query runs fine along. In any case, I do have a green underline on CREATE PROCEDURE, which let’s me know of a style issue.

2020-05-08 10_04_59-testquery1.sql - ARISTOTLE_SQL2017.Sandbox (ARISTOTLE_Steve (58))_ - Microsoft S

If I put the cursor on this line, I see the lightbulb in the left margin. Since this is available, I know I have a possible “fix” that SQL Prompt will handle.

2020-05-08 10_06_39-testquery1.sql - ARISTOTLE_SQL2017.Sandbox (ARISTOTLE_Steve (58))_ - Microsoft S

If I click this lightbulb, I see a few options.

2020-05-08 10_06_46-testquery1.sql - ARISTOTLE_SQL2017.Sandbox (ARISTOTLE_Steve (58))_ - Microsoft S

The top one is the one I want. When I click that, SQL Prompt will reformat my query and add the BEGIN and END.

2020-05-08 10_06_54-testquery1.sql - ARISTOTLE_SQL2017.Sandbox (ARISTOTLE_Steve (58))_ - Microsoft S

This can be controversial, as the END doesn’t stop things from being included in the procedure. For example, if I do this:

2020-05-08 10_08_48-testquery1.sql - ARISTOTLE_SQL2017.Sandbox (ARISTOTLE_Steve (58))_ - Microsoft S

I get this:

2020-05-08 10_09_09-testquery1.sql - ARISTOTLE_SQL2017.Sandbox (ARISTOTLE_Steve (58))_ - Microsoft S

The CREATE PROCEDURE needs to have the batch end to stop including code. The END doesn’t do this. Make sure you have the habit of ending your procedure creation with the batch separator (GO by default).

If you haven’t tried SQL Prompt, download an eval and see what you think. If you have it, upgrade and ensure you have all the code fixes.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , . Bookmark the permalink.