I'm complicated when it comes to putting together a stored procedure. The query is hardcodeed in php in this way:
if(trim($id) != ''){
$query = "select * from PACIENTES where idpac in (select pac_id from contacto where id = ".$id.") ";
}else{
$query = "select * from PACIENTES where 1=1 ";
if(trim($historia_clinica) != ''){
$query = $query." and historia_clinica_nro = '".$historia_clinica."'";
}
if(trim($documento) != ''){
$query = $query." and numero = '".$documento."'";
}
if(trim($nombre) != ''){
$query = $query." and nombre like '%".str_replace(' ', '%',$nombre)."%'";
}
if(trim($telefono) != ''){
$query = $query." and (ntelefo1 = '".$telefono."' or ntelefo2 = '".$telefono."' ) ";
}
if(trim($email) != ''){
//$query =$query." and mail = '".$email."'";
$query = $query." and hc_nro in (select idHistoriaClinica from paciente where mail ='".$email."')";
}
if(trim($id_paciente) != ''){
$query =$query." and id_paciente = '".$id_paciente."'";
}
}
I was writing something like this:
create sp_getPacientes
@id varchar(3),
@historia_clinica varchar(10),
@documento varchar(10),
@nombre varchar(20),
@telefono varchar(15),
@email varchar(30),
@id_paciente varchar(5)
as
begin
if @id is not null
select * from PACIENTES where id_paciente in (select paciente_id from
contacto where id = @id);
I need logic and an example, I do not ask you to solve everything. Thank you! Greetings