I'm working on my first stored procedure, and it's showing an error at the else (where I have a comment at the else below). If anyone knows how I can fix the error, I'd appreciate it. I've tried looking online, and I think it may have to do with my begin/end and if/else positioning. I'm having trouble finding a similar example though.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[SPName]
@userid NVARCHAR(51)
AS
SET NOCOUNT ON
DECLARE @return_status INT
IF @userid IS NULL OR @userid = 0
BEGIN
--fail
SET @return_status = 1
END
IF @return_status <> 1
BEGIN
IF EXISTS (SELECT login
FROM dbo.user_table
WHERE (@userid = login))
--continue because its a good user
ELSE
-- this is where it doesn't like the else
SET @return_status = 1
END
BEGIN
UPDATE dbo.U_MEMBER
SET dbo.U_MEMBER.inac = 1,
dbo.U_MEMBER.reg = @userid
FROM dbo.U_MEMBER
WHERE U_MEMBER.VF = 'REV'
END
RETURN 0
IF
, but you don't have a something to do if it's true. You go straight to anELSE
. AnIF
need something to do when theIF
evaluates to true. – LarnuSET @return_status=1
to fail the SP, because it doesn't. In fact, the second time is it set achieves nothing, as it's never referenced against afterwards. Seems like you might be better off withTHROW
for the firstIF
and a custom error message. – Larnu