SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Incorrect syntax near the keyword 'ELSE'. SQL 2008
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ezramod
Starting Member

12 Posts

Posted - 12/08/2012 :  09:44:10  Show Profile  Reply with Quote
hello i want to insert new update table in story procedure but when i execute appear this error:

Msg 156, Level 15, State 1, Procedure TT_IGScore, Line 14
Incorrect syntax near the keyword 'ELSE'.

This is my store procedure:

USE [Ranking]
GO
/****** Object:  StoredProcedure [dbo].[TT_IGScore]    Script Date: 12/08/2012 16:24:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE	[dbo].[TT_IGScore]
	@ServerCode		INT,
	@AccountID		VARCHAR(10),
	@Name			VARCHAR(10),
	@PlayDay		TINYINT
As
Begin
	BEGIN TRANSACTION
	SET NOCOUNT ON	

	IF EXISTS (SELECT NULL FROM IMPERIALGUARDIAN_INFO WHERE Account = @AccountID AND Name = @Name AND ServerCode = @ServerCode AND PlayDay = @PlayDay) 
		UPDATE IMPERIALGUARDIAN_INFO SET WinCount = WinCount+1 WHERE Account = @AccountID AND Name = @Name AND ServerCode = @ServerCode AND PlayDay = @PlayDay
		UPDATE MuOnline.dbo.Character SET reputation = reputation+1 WHERE AccountID = @AccountID AND Name = @Name
	ELSE
		INSERT INTO IMPERIALGUARDIAN_INFO (ServerCode, Account, Name, WinCount, PlayDay) VALUES (@ServerCode,@AccountID,@Name,1,@PlayDay)

	IF(@@Error <> 0 )
		ROLLBACK TRANSACTION
	ELSE	
		COMMIT TRANSACTION

	SET NOCOUNT OFF	
End


where is WRONG?

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/08/2012 :  10:26:50  Show Profile  Reply with Quote
You need a BEGIN and an END because the IF section has two statements; see in red below. The second BEGIN and END that I inserted are strictly speaking not necessary.
USE [Ranking]
GO
/****** Object:  StoredProcedure [dbo].[TT_IGScore]    Script Date: 12/08/2012 16:24:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[TT_IGScore]
	@ServerCode INT,
	@AccountID VARCHAR(10),
	@Name VARCHAR(10),
	@PlayDay TINYINT
AS
BEGIN
	BEGIN TRANSACTION
	SET NOCOUNT ON	
	
	IF EXISTS (
	       SELECT NULL
	       FROM   IMPERIALGUARDIAN_INFO
	       WHERE  Account = @AccountID
	              AND NAME = @Name
	              AND ServerCode = @ServerCode
	              AND PlayDay = @PlayDay
	)
	BEGIN
	    UPDATE IMPERIALGUARDIAN_INFO
	    SET    WinCount = WinCount + 1
	    WHERE  Account = @AccountID
	           AND NAME = @Name
	           AND ServerCode = @ServerCode
	           AND PlayDay = @PlayDay
	
	UPDATE MuOnline.dbo.Character
	SET    reputation = reputation + 1
	WHERE  AccountID = @AccountID
	       AND NAME = @Name
	END
   ELSE
   	BEGIN
	
	INSERT INTO IMPERIALGUARDIAN_INFO
	  (
	    ServerCode,
	    Account,
	    NAME,
	    WinCount,
	    PlayDay
	  )
	VALUES
	  (
	    @ServerCode,
	    @AccountID,
	    @Name,
	    1,
	    @PlayDay
	  )
	END
	IF (@@Error <> 0)
	    ROLLBACK TRANSACTION
	ELSE
	    COMMIT TRANSACTION
	
	SET NOCOUNT OFF
END
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000