Home > Software engineering >  If-Else condition does not work as I expect
If-Else condition does not work as I expect

Time:03-01

I have a code that does not seem to have any errors in the writing. The code tries to do joins between tables.

exec sel_historiapaciente 1
create proc sel_historiapaciente(
                @idpaciente paciente
)
as
set nocount on

if exists (select * from paciente p
            inner join historiapaciente hp
            on hp.idpaciente = p.idpaciente
            inner join historia h
            on h.idhistoria= hp.idhistoria
            inner join medicoespecialidad me
            on me.idmedico = hp.idmedico
            inner join medico m
            on m.idmedico = me.idmedico
            where p.idpaciente = @idpaciente)
begin
    select * from paciente p
    inner join historiapaciente hp
    on hp.idpaciente = p.idpaciente
    inner join historia h
    on h.idhistoria= hp.idhistoria
    inner join medicoespecialidad me
    on me.idmedico = hp.idmedico
    inner join medico m
    on m.idmedico = me.idmedico
    where p.idpaciente = @idpaciente
end
ELSE
begin
    select 0 as resultado
end

As I said above, the code works fine up to this point:

ELSE
begin
    select 0 as resultado
end

enter image description here

When idpaciente = 8, I have the result of the first figure, so far there are no problems. But when the number is different from idpaciente = 8, the code should show me a column with the value "0", and what it shows me is this:

enter image description here

CodePudding user response:

Add begin after the as keyword, and an end at the very end of the proc definition. In your syntax the proc is just set nocount on; it does nothing else.

CodePudding user response:

I solved the problem. I changed CREATE to ALTER, ran proc and now it works as it should

  • Related