Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function creation fails on else if statement 1

Status
Not open for further replies.

Kalin

Programmer
Jul 24, 2001
76
NL
Hi all,

I'm creating a function, it works when I remove the last or second last else if statement. But fails (Incorrect syntax near the keyword 'ELSE' on line 14) when I include both.

Source:

Alter Function fn_SwapRuimte (@LijnID nvarchar(20))
RETURNS @fn_SwapRuimte table
(Origin nvarchar(20) NOT NULL,
Destin nvarchar(20) )
AS
BEGIN
IF patindex('%-%',@LijnID) = 0
INSERT @fn_SwapRuimte SELECT @LijnID, null
ELSE IF patindex('%-%',@LijnID) = 3
INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 2, 1) + substring(@LijnID, 1, 1) + '-'+ substring(@LijnID, 4, LEN(@LijnID) - 3)
ELSE IF patindex('%-%',@LijnID) = 4
INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 3, 1) + substring(@LijnID, 1, 2) + '-'+ substring(@LijnID, 5, LEN(@LijnID) - 4)
INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 2, 2) + substring(@LijnID, 1, 1) + '-'+ substring(@LijnID, 5, LEN(@LijnID) - 4)
ELSE IF patindex('%-%',@LijnID) = 5
INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 4, 1) + substring(@LijnID, 1, 3) + '-'+ substring(@LijnID, 6, LEN(@LijnID) - 5)
INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 3, 2) + substring(@LijnID, 1, 2) + '-'+ substring(@LijnID, 6, LEN(@LijnID) - 5)
INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 2, 3) + substring(@LijnID, 1, 1) + '-'+ substring(@LijnID, 6, LEN(@LijnID) - 5)
RETURN
END

Any suggestions are welcome.


Grtz,

Kalin
 
You need BEGIN...END around them as there is more than one line to execute:

Code:
Alter Function fn_SwapRuimte (@LijnID nvarchar(20))
RETURNS @fn_SwapRuimte table
  (Origin nvarchar(20) NOT NULL,
   Destin nvarchar(20) )
AS
BEGIN
IF patindex('%-%',@LijnID) = 0
  INSERT @fn_SwapRuimte SELECT @LijnID, null
ELSE IF patindex('%-%',@LijnID) = 3
  INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 2, 1) + substring(@LijnID, 1, 1) + '-'+ substring(@LijnID, 4, LEN(@LijnID) - 3)
ELSE IF patindex('%-%',@LijnID) = 4
BEGIN
  INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 3, 1) + substring(@LijnID, 1, 2) + '-'+ substring(@LijnID, 5, LEN(@LijnID) - 4)
  INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 2, 2) + substring(@LijnID, 1, 1) + '-'+ substring(@LijnID, 5, LEN(@LijnID) - 4)
END
ELSE IF patindex('%-%',@LijnID) = 5
BEGIN
  INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 4, 1) + substring(@LijnID, 1, 3) + '-'+ substring(@LijnID, 6, LEN(@LijnID) - 5)
  INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 3, 2) + substring(@LijnID, 1, 2) + '-'+ substring(@LijnID, 6, LEN(@LijnID) - 5)
  INSERT @fn_SwapRuimte SELECT @LijnID, substring(@LijnID, 2, 3) + substring(@LijnID, 1, 1) + '-'+ substring(@LijnID, 6, LEN(@LijnID) - 5)
END
RETURN
END

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top