此语句能优化吗?
insert into yfcc(票号,序号,小号,产品编号,库房,批号,效期,标识,库房数量,数量,件数,保管员,审核时间,业务1,业务2,业务3,业务4,业务5,单价,金额) select top 1 @p,@x, '1 ',kc.产品编号,库房,批号,效期,标识,库房数量,(case when 库房= 'dk1 ' then (case when kc.产品编号 in(select 产品编号 from cp where 功效 like 'A% ') then cp.包装 else round(cp.包装/2,0) end) else (case when kc.产品编号 in(select 产品编号 from cp where 功效 like 'A% ') then cp.包装*2 else cp.包装 end) end), '1 ',@kh1,getdate(), ' ', ' ', ' ', ' ', ' ',round(cp.平均进价,2),round(round(cp.平均进价,2)*(case when 库房= 'dk1 ' then (case when kc.产品编号 in(select 产品编号 from cp where 功效 like 'A% ') then cp.包装 else round(cp.包装/2,0) end) else (case when kc.产品编号 in(select 产品编号 from cp where 产品编号=kc.产品编号 and 功效 like 'A% ') then cp.包装*2 else cp.包装 end) end),2) from kc inner join cp on cp.产品编号=kc.产品编号 and (select 客户全称 from kh where 客户编码=@kh1) like left(cp.产地,5)+ '% ' and (left(批号,6) between substring(convert(varchar(8),cast(convert(varchar,(@yyy+@ddd),112) as datetime)-30*cast(6+rand()*(9-6) as int),112),3,6) and substring(convert(varchar(8),cast(convert(varchar,(@yyy+@ddd),112) as datetime)-5,112),3,6)) and cast(convert(varchar,(@yyy+@ddd),112) as datetime) <效期 and kc.产品编号 not in(select 产品编号 from yfcc where 保管员=@kh1 and substring(票号,5,8)=left(convert(char,(@yyy+@ddd),112),8)) order by newid()
------解决方案--------------------in(select 产品编号 from cp where 功效 like 'A% ')
------------------------
三个一样,另一个类似,多加个条件而已
declare @temp table (产品编号 ???)
insert @temp select 产品编号 from cp where 功效 like 'A% '
try:用@temp代替。