IF...ELSE error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hannibal111111@hotmail.com

    IF...ELSE error

    Hi,

    I am trying to print out a statement if a record is not found, but I
    keep getting Error 156: incorrect syntax near Else. Here is the stored
    procedure:

    CREATE PROCEDURE AddUpdateDoorSt yles

    (
    @myprojectid varchar(255),
    @doorstyle varchar(255)
    )

    AS

    IF EXISTS (select *
    from myproject_doors tyles
    where myproject_id = @myprojectid and door_style = @doorstyle)
    ELSE
    Print 'test'

    GO

    Any ideas?

    Thanks,

    Chris

  • akej via SQLMonster.com

    #2
    Re: IF...ELSE error

    >Any ideas?


    CREATE PROCEDURE AddUpdateDoorSt yles

    (
    @myprojectid varchar(255),
    @doorstyle varchar(255)
    )

    AS

    IF EXISTS (select *
    from myproject_doors tyles
    where myproject_id = @myprojectid and door_style = @doorstyle)

    Print 'ok'
    ELSE
    Print 'notOK'


    ----------------
    or u can use

    IF NOT EXISTS (select *
    from myproject_doors tyles
    where myproject_id = @myprojectid and door_style = @doorstyle)

    Print 'notOK'




    GO

    --
    Message posted via http://www.sqlmonster.com

    Comment

    • Erland Sommarskog

      #3
      Re: IF...ELSE error

      (Hannibal111111 @hotmail.com) writes:[color=blue]
      > I am trying to print out a statement if a record is not found, but I
      > keep getting Error 156: incorrect syntax near Else. Here is the stored
      > procedure:
      >
      > CREATE PROCEDURE AddUpdateDoorSt yles
      >
      > (
      > @myprojectid varchar(255),
      > @doorstyle varchar(255)
      > )
      >
      > AS
      >
      > IF EXISTS (select *
      > from myproject_doors tyles
      > where myproject_id = @myprojectid and door_style = @doorstyle)
      > ELSE
      > Print 'test'[/color]


      IF ELSE with nothing in between is not permitted in T-SQL, so you need
      to put in some dummy statement or change EXISTS to NOT EXISTS:


      --
      Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

      Books Online for SQL Server SP3 at
      Get the flexibility you need to use integrated solutions, apps, and innovations in technology with your data, wherever it lives—in the cloud, on-premises, or at the edge.

      Comment

      Working...