2013-07-11 106 views
1
select (select code from SC_StockItem where StockItemID = a.StockItemID) 
from SC_StockItemVariantSetAttribValue a 
inner join SC_StockItemVariantSetAttribValue a2 on a.StockItemID = a2.StockItemID 
and a2.VariantSetAttribValueID = 18 
inner join SC_StockItemVariantSetAttribValue a3 on a2.StockItemID = a3.StockItemID 
and a3.VariantSetAttribValueID = 12 
where a.VariantSetAttribValueID = 26 

我希望執行上面的查詢。類似於:用tsql創建動態sql?

inner join SC_StockItemVariantSetAttribValue a2 on a.StockItemID = a2.StockItemID 
    and a2.VariantSetAttribValueID = 18 

將相應地添加到從另一個查詢返回的行數中。有什麼方法可以在TSQL函數中添加這些行嗎?

+0

沒有動態SQL適合? – revoua

+0

這是用於另一個功能,因爲它已經有點要求在服務器上,我寧願如果我可以避免多次調用從C#到SQL服務器 – Jonny

+0

動態SQL的最佳信息:[動態SQL](http:///www.sommarskog.se/dynamic_sql.html)。這應該回答關於動態SQL的所有內容(少數優於SO的資源之一;-))。但是你的其他查詢又是如何查看的?也許這兩個查詢結合起來更容易? –

回答

0

嘗試添加條件join使這部分固定,不DINAMIC:

inner join SC_StockItemVariantSetAttribValue a2 on (a.StockItemID=a2.StockItemID 
and a2.VariantSetAttribValueID = 19 
and @anotherQueryCount > 0) --here you check your additional rule 

加入,如果條件爲真會奏效。