Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax near 'end of file.' Expecting CONVERSATION, or TRY

Tags:

sql-server

I'm very new at this. I've been messing around with some T-SQL and now for the hell of it I'm doing some basic password stuff. Now, when I did this, as you'll see in the code, I have it set to try and throw an error if you use a duplicate username, and if it finds no issue and creates the account, it messages "User created" or something of the sort. That failed, and while the primary key did prevent an insert of a duplicate username, it would increment the identity column anyways, leaving me with 2 users, one with UserID 1, and another with UserID 17. Additionally, it would not throw up an error, simply saying command executed. So, to try and force an error, I've done some probably very spaghetti things to my code, and now it's giving me an incorrect syntax error at the very end.

The exact error is as in the title, "Incorrect syntax near 'end of file.' Expecting CONVERSATION, or TRY. " and I have no idea why. I've searched around a bit and I can't seem to find anyone else with this error, or understand why it would expect either of those things, so I'm lost. Bask in the glory of my spaghetti code; if you can think of a way to fix the initial issue of not throwing an error from a stored procedure rather than fixing the incorrect syntax error, that'd be welcome too.

ALTER procedure [dbo].[adduser]
    @pUsername nvarchar(50),
    @pPassword nvarchar(50),
    @responsemessage nvarchar(250) output,
    @saveme nvarchar(30) = 'Duplicate username' 
as 
set nocount on
begin


    declare @salt UNIQUEIDENTIFIER=newid()
        begin tran

        begin try
            if exists ((select Username from UserLogins where Username = @pUsername)) 
                begin  
                    rollback tran 
                    raiserror(1337, 16, @saveme)
                end   
                else 
                    begin try

                        insert into dbo.UserLogins (Username, PasswordHash, Salt)
                        values(@pUsername, hashbytes('SHA2_512', @pPassword+cast(@salt as nvarchar(36))), @salt)
                            set @responsemessage='User created'

                    end try 
                    begin catch
                        set @responsemessage = ERROR_MESSAGE()
                    end catch  

end
like image 711
Lordson Avatar asked Oct 28 '25 01:10

Lordson


1 Answers

Each BEGIN statement should have a subsequent END(Closing) statement, which is the cause of the said error. As commented by Lordson, he missed placing a closing COMMIT TRAN for his BEGIN TRAN statement.

like image 109
Dhiraj Singh Avatar answered Oct 30 '25 19:10

Dhiraj Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!