Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help finding unknown error in code.

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-04-07 : 16:43:41
I am trying to make a stored proc and SQL keeps telling me there is "Incorrect syntac near 'END'" on line 47, which is the last line of code. I do not see anything wrong, can I get second set of eyes please? Thanks

CREATE PROC spMemberInfo
@UpdateType AS char(6) = NULL,
@UserName AS varchar(256) = NULL,
@Email AS varchar(255) = NULL,
@WebSite AS varchar(255) = NULL,
@ContactList AS char(1) = NULL,
@IsStaff AS bit = NULL
AS

SET NOCOUNT ON;
SET XACT_ABORT ON;

DECLARE @ErrorNum int;

BEGIN TRANSACTION

--Process a new registration attempt
IF (@UpdateType = 'Insert')
BEGIN
--Check to see if the member is already registered
IF NOT EXISTS (SELECT TOP 1 1 FROM dbo.MemberInfo WITH (NOLOCK) WHERE UserName = @UserName)
BEGIN
INSERT INTO dbo.MemberInfo (UserName, Email, WebSite, ContactList, IsStaff)
VALUES (@UserName, @Email, @WebSite, @ContactList, @IsStaff);

SELECT @ErrorNum = @@ERROR;
END
ELSE
--Process a profile update
BEGIN
UPDATE dbo.MemberInfo
SET Email = @Email,
WebSite = @WebSite,
ContactList = @ContactList,
IsStaff = @IsStaff
WHERE UserName = @UserName;
END

--Check for errors and commit if none
IF (@ErrorNum = 0)
BEGIN
COMMIT
END
ELSE
BEGIN
ROLLBACK
END


--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2012-04-07 : 17:23:32
You are missing an END to match this BEGIN:

IF (@UpdateType = 'Insert')
BEGIN

More worrying though is this: @ErrorNum is only set if @UpdateType = 'Insert'.

That means @ErrorNum will be NULL for any other value of @UpdateType, so your transaction will always be rolled back in that case. Because @ErrorNum = 0 will be false if @ErrorNum is NULL.

that means your block starting with "--Process a profile update" will always be rolled back, even if there's no error.




elsasoft.org
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2012-04-07 : 17:35:06
btw since you are using XACT_ABORT ON, you don't need to be checking @@ERROR at all. the server will rollback automatically if an error occurs.

See Peter's writeup here:

http://weblogs.sqlteam.com/peterl/archive/2009/04/07/ERROR-BEGIN-TRYCATCH-and-XACT_ABORT.aspx


elsasoft.org
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2012-04-07 : 17:36:23
Thanks on catching the missing end and the missing @Error in the else.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page
   

- Advertisement -