Valid T-SQL syntax?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DFS

    Valid T-SQL syntax?

    Version 1:
    --------------------------------
    If Condition = A
    UPDATE query 1
    UPDATE query 2
    ELSE IF Condition = B
    UPDATE query



    Version 2:
    --------------------------------
    If Condition = A
    BEGIN
    UPDATE query 1
    UPDATE query 2
    END
    ELSE IF Condition = B
    BEGIN
    UPDATE query
    END


    Or are they functionally equivalent?

    Thanks




  • Simon Hayes

    #2
    Re: Valid T-SQL syntax?


    "DFS" <nospam@nospam. com> wrote in message
    news:101ovu67bf qpr6b@corp.supe rnews.com...[color=blue]
    > Version 1:
    > --------------------------------
    > If Condition = A
    > UPDATE query 1
    > UPDATE query 2
    > ELSE IF Condition = B
    > UPDATE query
    >
    >
    >
    > Version 2:
    > --------------------------------
    > If Condition = A
    > BEGIN
    > UPDATE query 1
    > UPDATE query 2
    > END
    > ELSE IF Condition = B
    > BEGIN
    > UPDATE query
    > END
    >
    >
    > Or are they functionally equivalent?
    >
    > Thanks
    >
    >
    >
    >[/color]

    See the Books Online entry for IF ... ELSE ... Version 1 is not valid
    syntax - IF must be followed by a single SQL statement, or by a block of
    statements enclosed in BEGIN ... END. Version 2, on the other hand, is
    correct. Try it yourself in Query Analyzer, and you should get a syntax
    error using version 1.

    Simon


    Comment

    • DFS

      #3
      Re: Valid T-SQL syntax?


      "Simon Hayes" <sql@hayes.ch > wrote in message
      news:401c9ff8$1 _2@news.bluewin .ch...[color=blue]
      >
      > "DFS" <nospam@nospam. com> wrote in message
      > news:101ovu67bf qpr6b@corp.supe rnews.com...[color=green]
      > > Version 1:
      > > --------------------------------
      > > If Condition = A
      > > UPDATE query 1
      > > UPDATE query 2
      > > ELSE IF Condition = B
      > > UPDATE query
      > >
      > >
      > >
      > > Version 2:
      > > --------------------------------
      > > If Condition = A
      > > BEGIN
      > > UPDATE query 1
      > > UPDATE query 2
      > > END
      > > ELSE IF Condition = B
      > > BEGIN
      > > UPDATE query
      > > END
      > >
      > >
      > > Or are they functionally equivalent?
      > >
      > > Thanks
      > >
      > >
      > >
      > >[/color]
      >
      > See the Books Online entry for IF ... ELSE ... Version 1 is not valid
      > syntax - IF must be followed by a single SQL statement, or by a block of
      > statements enclosed in BEGIN ... END. Version 2, on the other hand, is
      > correct. Try it yourself in Query Analyzer, and you should get a syntax
      > error using version 1.
      >
      > Simon[/color]

      Thanks. Yes, I tried it last night and it's just as you say.




      Comment

      Working...