Tabelele temporare sunt şterse automat la sfârşitul procedurii în care au fost create. În cazul în care se tabela temporară se crează folosind dynamic sql se consideră că acele instrucţiuni fac parte dintr-o "procedură" separată, iar tabela temporară se şterge atunci când se revine în procedura iniţială.
În orice caz, eu aş rescrie cel puţin sfârşitul procedurii (ceea ce e după EXEC), astfel:
select * from temp3 as x
left outer join (
select prototype_model_id_1, prototype_model_id_1 as grup, model_type_lien
from model_lie
union
select b.prototype_model_id_2, a.prototype_model_id_1, a.model_type_lien
from (
select distinct prototype_model_id_1, model_type_lien
from model_lie
) a, model_lie b where a.prototype_model_id_1=b.prototype_model_id_1
) as y
on x.PROTOTYPE_model_id=y.prototype_model_id_1
Cred (dar nu sunt sigur) că asta ar fi echivalent (dacă prototype_model_id_2 nu e NULL) cu:
select * from temp3 as x
left outer join (
SELECT DISTINCT ISNULL(b.prototype_model_id_2,a.prototype_model_id_1) as prototype_model_id_1,
prototype_model_id_1 as grup, model_type_lien
FROM model_lie a LEFT JOIN model_lie b ON a.prototype_model_id_1=b.prototype_model_id_1
) as y
on x.PROTOTYPE_model_id=y.prototype_model_id_1
Răzvan