Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/08/2013, 12:00
Avatar de Killerx_8937
Killerx_8937
 
Fecha de Ingreso: noviembre-2006
Mensajes: 99
Antigüedad: 17 años, 5 meses
Puntos: 0
Respuesta: Procedimiento almacenado para busqueda

Estimados habra una forma mas eficiente de hacer esto....

Código SQL:
Ver original
  1. CREATE PROCEDURE SpTest(IN Run VARCHAR(12),IN Empresa VARCHAR(12))
  2. BEGIN
  3. DECLARE condicion VARCHAR(100);
  4. SET condicion = "";
  5. IF Run <> "" THEN
  6.     IF condicion = " " THEN
  7.         SET condicion = CONCAT(' WHERE UsuarioRun=''', Run,'''');
  8.     ELSE
  9.         SET condicion = CONCAT(' AND  UsuarioRun=''', Run,'''');
  10.     END IF;    
  11. END IF;
  12. IF Empresa <> "" THEN
  13.     IF condicion = " " THEN
  14.         SET condicion = CONCAT(' WHERE UsuarioEmpresa=''', Empresa,'''');
  15.     ELSE
  16.         SET condicion = CONCAT(condicion,' AND UsuarioEmpresa=''', Empresa,'''');
  17.     END IF;    
  18. END IF;
  19. SET @consulta = CONCAT("select * from Sis_Usuario ", condicion);
  20. PREPARE stmt FROM @consulta;
  21. EXECUTE stmt;
  22. END;