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!