Help wanted with SQL syntax

AlignAlign Remain Calm Join Date: 2002-11-02 Member: 5216Forum Moderators, Constellation
<div class="IPBDescription">In stored procedures specifically</div>So me and my teammates are doing an assignment, but have run into a little problem with the syntax. Semicolons have to be put in strange places and linebreaks (without semicolons) actually make a difference. We're forced to guess our way through the construction of the procedures, which is obviously not good. The following bit actually works, but it looks like hell and doesn't help us with the later questions in the assignment:
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->create procedure
Deposit(in aamount integer, anumber integer)
modifies sql data
BEGIN
    if( aamount >= 0) then
        update account
        set balance = balance+aamount
        where number = anumber
    else
   ; end if
; END;<!--c2--></div><!--ec2-->
Moving the semicolons so they appear in more reasonable places breaks the code. Same with removing the else, but that might be normal for SQL.

Comments

  • lolfighterlolfighter Snark, Dire Join Date: 2003-04-20 Member: 15693Members
    edited June 2008
    This isn't meant to be pedantic, but keep in mind that the S stands for Structured, not Standardised. Implementations have subtle differences, so it might help to specify what you're working with.

    Edit: What type of database, that is.
  • TheslanTheslan TWG Signature Maker Join Date: 2004-04-27 Member: 28245Members
    DISCLAIMER: I don't remember SQL syntax very well.

    I have to ask... does the whitespace matter for the semicolon under else (<img src="style_emoticons/<#EMO_DIR#>/wink-fix.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink-fix.gif" /> end if?

    Does it matter if it's something like

    else
    ;
    end if

    That looks very reasonable to me, considering that the semi-colon could be used to indicate that it is at the end of the else statement. Dunno... maybe the if statement needs one too?

    I'll expect something like...

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->    if( aamount >= 0) then
            update account;
            set balance = balance+aamount
            where number = anumber;
        else
       ;
       end if<!--c2--></div><!--ec2-->

    But that's just the theory of languages... we do really need to know what kind of database this is.
  • AlignAlign Remain Calm Join Date: 2002-11-02 Member: 5216Forum Moderators, Constellation
    <!--quoteo(post=1681064:date=Jun 13 2008, 01:16 PM:name=lolfighter)--><div class='quotetop'>QUOTE(lolfighter @ Jun 13 2008, 01:16 PM) <a href="index.php?act=findpost&pid=1681064"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->This isn't meant to be pedantic, but keep in mind that the S stands for Structured, not Standardised. Implementations have subtle differences, so it might help to specify what you're working with.

    Edit: What type of database, that is.<!--QuoteEnd--></div><!--QuoteEEnd-->
    Damn, I knew I forgot something.
    Mimer.
  • AlignAlign Remain Calm Join Date: 2002-11-02 Member: 5216Forum Moderators, Constellation
    edited June 2008
    <!--quoteo(post=1681080:date=Jun 13 2008, 05:59 PM:name=Theslan)--><div class='quotetop'>QUOTE(Theslan @ Jun 13 2008, 05:59 PM) <a href="index.php?act=findpost&pid=1681080"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec--><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->    if( aamount >= 0) then
            update account
            set balance = balance+aamount
            where number = anumber;
        else
    ;
       end if<!--c2--></div><!--ec2--><!--QuoteEnd--></div><!--QuoteEEnd-->I can't say for 100% sure without trying it in the lab room but I don't think that would work. Linebreaks weren't accepted without a semicolon at the end.

    EDIT: Nope. Semicolon after anumber:
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->MIMER/DB error -12740 in function PREPARE
             Syntax error, <end> assumed to mean; END IF; END <end><!--c2--></div><!--ec2-->

    Even removing the linebreaks leaves error:
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->create procedure
    Deposit(in aamount integer, anumber integer)
    modifies sql data
    BEGIN
        if( aamount >= 0) then
            update account
            set balance = balance+aamount
            where number = anumber;    else      end if; END;
    8:                                                           1
    MIMER/DB error -12740 in function PREPARE
             Syntax error, end assumed to mean CALL <identifier> ( ); end
    SQL><!--c2--></div><!--ec2-->
Sign In or Register to comment.