+ Reply to Thread
Results 1 to 4 of 4

Thread: BEGIN END useage Stored Procedures

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    BEGIN END useage Stored Procedures

    What's the meaning of the BEGIN...END statements in Stored Procedures?

    At the moment, I just have a BEGIN at the start and an END at the finish, but I've seen several examples where BEGIN..END is used inside IF..THEN statements
    Code:
    If(condition=true) THEN
    BEGIN
    ....
    END
    ELSE
    BEGIN
    ....
    END
    END IF;
    
    Whilst I was googling the use of BEGIN..END I then discovered this thread albeit about MSSQL, I presume, where several posts say they don't use them at all.

    What is the advantage / disadvantage of using them and what is the most appropriate way to use them? One block or seperated by conditions?

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    BEGIN..END is most used in IF statements. You can think of it exactly like the { } in C# or like IF...THEN...END IF in BASIC. Basically, the BEGIN...END sets off multiple SQL statements to be executed as a block if the condition is TRUE (in an IF statement for example).

    While you can use BEGIN..END around the entire stored procedure, but there is really no need in this case.

    Note that this is true for T-SQL and is probably close to ASNI SQl, so MySql could be different. For example, there is no END IF statement in T-SQL.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Thanks Wolffy....Is there any advantage over:-
    a) using BEGIN...END statements in the first place? See link
    b) using seperate BEGIN...END statements for each condition?

    IE, is there any advantage (performance or otherwise) in using
    Code:
    IF
    BEGIN
    .
    END
    ELSE
    BEGIN
    .
    END
    END IF
    
    over
    Code:
    BEGIN
    IF
    .
    ELSE
    .
    END IF
    END
    

  4. #4
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    I can see no advantage of using a BEGIN..END around the entire procedure -- I'd call that a matter of personal preference. I then to put a BEGIN after I declare my procedure variables, but there is no syntactical reason to do so.

    The BEGIN..END is required for code blocks in the IF statement, unless the code block has only a single statement; as in your first example. Now, MySql may have extended ANSI to allow the IF...ELSE...END IF syntax (don't KNOW that, don't use MySql). However, I'd consider sticking to the ANSI standard where possible in case you ever need to migrate from MySql to MSSQL Server.

    In your second example, the BEGIN..END servers no purpose that I can see.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. Replies: 21
    Last Post: July 14th, 2009, 12:58 PM
  2. help on stored procedures?
    By Centurion in forum Microsoft SQL Server
    Replies: 10
    Last Post: June 17th, 2009, 10:40 AM
  3. Executing Stored Procedures in Access VBA
    By TedMosby in forum Microsoft Access
    Replies: 20
    Last Post: May 15th, 2009, 12:49 PM
  4. Stored Procedures on MySQL
    By richyrich in forum MySQL
    Replies: 7
    Last Post: June 9th, 2008, 12:15 PM
  5. Permissions on Tables, Stored Procedures, etc.
    By theChris in forum Microsoft SQL Server
    Replies: 2
    Last Post: March 24th, 2008, 12:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO