用tsql创建动态sql?

时间:2013-07-11 13:55:33

标签: sql sql-server tsql dynamic-sql

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

我希望执行上面的查询。线条类似于:

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

将相应地添加到另一个查询返回的行数。有没有办法在TSQL函数中添加这些行?

1 个答案:

答案 0 :(得分:0)

尝试向join添加条件,使这部分固定,而不是动态:

inner join SC_StockItemVariantSetAttribValue a2 on (a.StockItemID=a2.StockItemID
and a2.VariantSetAttribValueID = 19 
and @anotherQueryCount > 0)  --here you check your additional rule  
如果条件为真,

加入将起作用。