由于我不知道实际查询中表的架构,下面是预先创建表的诀窍。
select top 0 im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
into #Articol
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184
@Sql NVarchar(max)
set @Articol = 99190
set @Sql=N'
insert into #Articol
select im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184'
IF @Articol <> -1 set @Sql = @Sql + ' and im.ItemId = @Articol'
If @Articol = -1 set @Sql = @Sql
exec sp_executesql @Sql, N'@Articol int', @Articol
select * from #Articole
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。