Is there any short cut way to find out currosponding BEGIN and END ?

  • Hi,

    I m creating one stored procedure which is around 2000 lines .In this stored procedure I have lot of nested IF, ELSE IF conditions. For each IF there is BEGIN and END. But after creating the stored procedure it is very difficult to track which END belongs to which BEGIN in spite of having good indentation.

    Is there any short cut way to find out corresponding BEGIN and END?

    Yuvraj

     

  • I find indentation useful:

    If @Var1 = 1

    Begin

        If @Var2 = 1

        Begin

            Select ....

        End

        Else

        Begin

            Select...

        End

    End

    Else

    Begin

        Select ...

    End

    Also if I know the stored procedure is going to be complex when I begin writing I put a comment at the 'begin' statement and corresponding statement at the 'End statement. For example:

    If @option = 1

    begin

    /* Option 1 selected.. */

           Select field from table etc..

    /* End.. Option 1 selected */

    End

    Else If @option = 2

    Begin

    /* Option 2 selected.. */

           Select field from table etc..

    /* End.. Option 2 selected */

    End

    Else

    Begin

    /* Option > 2 selected.. */

           Select field from table etc..

    /* End.. Option > 2 selected */

    End

    Helps a great deal when you come back a year later

     

     

     

     

     

  • The commenting method shown above, or some derivative, is the best way. There is no automated way.

  • Indenting and commenting is definitely the best way to go no matter what. But you could always look at an editor that helps you with things like this. I use ApexSQLEdit (there may be others that do similar things) and the "outlining" feature helps to make sense of the code a bit better--even if it isn't indented properly. For instance, even if everything is left justified you could use the outlining to collapse and expand sections of code. This might give you a hand. If you are interested just check out http://www.apexsql.com.

    I am in no way affiliated with this company. I just happen to like their editor

    George

  • Hi George,

    I'm an old IDMS ADS/O programmer, so I know the feeling. I pulled this method along with me through the years. The easiest way I know to track the levels is putting the comment of the level number at each BEGIN and END. For example

    IF (...)

      BEGIN --1

        stmt1

        stmt2

        WHILE (...)

          BEGIN --2

            stmt3

            stmt4

          END --2

        stmt5

        stmt6

      END  --1

     

    The best way to validate this is to use QA or Notepad and just search on the BEGINs or ENDs to verify the levels.

     

    Regards,

    Larry

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply