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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help finding unknown error in code.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
384 Posts

Posted - 04/07/2012 :  16:43:41  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2875 Posts

Posted - 04/07/2012 :  17:23:32  Show Profile  Visit jezemine's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2875 Posts

Posted - 04/07/2012 :  17:35:06  Show Profile  Visit jezemine's Homepage  Reply with Quote
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

USA
384 Posts

Posted - 04/07/2012 :  17:36:23  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
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
  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.05 seconds. Powered By: Snitz Forums 2000