como optimizar esto???

15/11/2003 - 15:47 por Tolo | Informe spam
Hola

estoy haciendo una función qeu me devuelve el precio de un articulo a través
de la variable @pv, para ello necisto hacer el cálculo como:

if not exists (select @pv = preu_especial from t_PreusEspecials where
fk_article=@article and fk_tercer=@tercer)
select @pv = preu_venta from t_articles where id_article = @article

Pero no puedo en el mismo select q miro si existe asignar el valor a mi
variable, y tengo q hacer ...

if exists (select preu_especial from t_PreusEspecials where
fk_article=@article and fk_tercer=@tercer)
select @pv = preu_especial from t_PreusEspecials where
fk_article=@article and fk_tercer=@terce
else
select @pv = preu_venta from t_articles where id_article = @article


hay forma de mejorar esto???

gracias de nuevo
 

Leer las respuestas

#1 Javier Loria
15/11/2003 - 16:57 | Informe spam
Hola Tolo:
Sin el esquema de las tablas es mucho mas dificil.
Si pretendes crear una funcion escalar con esto, vas a matar el
desempeno de SQL.
Prueba con esto:
/* Codigo SIN PROBAR y SIN ESQUEMA :( */
SELECT COALESCE(preu_especial,preu_venta)
FROM T_Articles
LEFT JOIN T_PreusEspecials
ON T_Articles.id_article=T_PreusEspecials.fk_article
T_PreusEspecials.fk_tercer=@tercer
WHERE T_Articles.id_article=@article
/* Fin de Codigo SIN PROBAR Y SIN ESQUEMA */

Si creas una funcion en Linea (eliminando el filtro del Article) y
agregando algunas columnas podrias usarlo como una vista parametrizada con
buen desemepeno.
Saludos,


Javier Loria
Costa Rica
Se aprecia la inclusion de DDL (CREATE, INSERTS, etc.)
que pueda ser copiado y pegado al Query Analizer.
La version de SQL y Service Pack tambien ayuda.
Tolo escribio:
Hola

estoy haciendo una función qeu me devuelve el precio de un articulo a
través de la variable @pv, para ello necisto hacer el cálculo como:

if not exists (select @pv = preu_especial from t_PreusEspecials where
fk_article=@article and fk_tercer=@tercer)
select @pv = preu_venta from t_articles where id_article > @article

Pero no puedo en el mismo select q miro si existe asignar el valor a
mi variable, y tengo q hacer ...

if exists (select preu_especial from t_PreusEspecials where
fk_article=@article and fk_tercer=@tercer)
select @pv = preu_especial from t_PreusEspecials where
fk_article=@article and fk_tercer=@terce
else
select @pv = preu_venta from t_articles where id_article > @article


hay forma de mejorar esto???

gracias de nuevo

Preguntas similares