Syntax error near "Else" SQL Server

1

Good evening / days / evenings.

I have been doing a stored procedure in which I insert data if it does not already exist within the different tables. The problem is that I have the idea of how to do it but now I have a problem that is the following.

CREATE PROCEDURE SP_Verificar_Dirección(
@localidad varchar(20),
@municipio varchar(20),
@estado varchar(20),
@status_localidad int,
@status_municipio int,
@status_estado int 
)
AS
BEGIN
DECLARE @id_L INT
DECLARE @id_M INT
DECLARE @id_E INT
IF NOT EXISTS (SELECT L.id_localidad FROM LOCALIDAD L WHERE L.nombre = @localidad)
BEGIN
    EXEC SP_Insertar_Localidad @localidad,@status_localidad
    SET @id_L =  (SELECT L.id_localidad FROM Localidad L, STATUS_TABLAS S WHERE L.id_status= S.id_status AND L.nombre = @localidad)
    SELECT @id_L
END     
        IF NOT  EXISTS (SELECT M.id_municipio FROM MUNICIPIO M WHERE M.nombre = @municipio)
        BEGIN
            EXEC SP_Insertar_Municipio @municipio,@id_L,@status_municipio
            SET @id_M = (SELECT M.id_municipio FROM MUNICIPIO M, LOCALIDAD L , STATUS_TABLAS S WHERE L.id_localidad = @id_L AND M.nombre = @municipio AND S.id_status = @status_municipio  )
            SELECT @id_M
        END

            IF NOT  EXISTS (SELECT E.id_estado FROM ESTADO E WHERE E.nombre = @estado)
            BEGIN
                EXEC SP_Insertar_Estado @estado,@id_M,@status_estado
                SET @id_E = (SELECT E.id_estado FROM ESTADO E, MUNICIPIO M, STATUS_TABLAS S WHERE M.id_municipio = @id_M AND E.nombre = @estado AND S.id_status = @status_estado)
            END


            ELSE 
            BEGIN
                SELECT 'Ya existe este estado'
            END

        ELSE 
        BEGIN
            SELECT 'Ya existe  ese municipio'
        END

ELSE
BEGIN
    SELECT 'YA existe esa localidad'
END
END
GO

The problem is that I put this error message:

I can not see how the syntaxys of the if's would be done since I imagined it was how I put it.

Thank you very much for your help and forgive the inconvenience.

Greetings!

    
asked by Kris Phoenix 19.11.2018 в 05:31
source

1 answer

0

You have an error in the syntax.

The if / else syntax is basically this:

if condicion
  SentenciaCumple
else
  SentenciaNoCumple

If within one of these sections you want to execute several statements, then you use blocks begin / end , for example:

if condicion
begin
  SentenciaCumple1
  SentenciaCumple2
end
else
begin
  SentenciaNoCumple1
  SentenciaNoCumple2
end

You can nest new if 's both within the if and within the else , but that is where you have failed. I have not followed the logic of your code, but speaking in a general way, you should always look something like:

if condicion
begin
  SentenciaCumple1
  SentenciaCumple2
  -- acá hago otra condicional, pero está contenida completa dentro de este bloque
  -- busca el comentario al final para identificarlo
  if OtraCondicion
  begin
    OtraSentenciaCumple1
    OtraSentenciaCumple2
  end
  else
  begin
    OtraSentenciaNoCumple1
    OtraSentenciaNoCumple2
  end
  -- acá termina el if anidado, pueden haber sentencias posteriores
  SentenciaCumple3
end
else
begin
  SentenciaNoCumple1
  SentenciaNoCumple2
end
    
answered by 19.11.2018 / 05:54
source