22,210
社区成员
发帖
与我相关
我的任务
分享
set @temp = '##ddd'
-------------------增加
declare @sql_t nvarchar(1000)
set @sql_t = 'select style from ['+@temp+']'
if OBJECT_ID('tempdb..#temp_123') is not null
drop table #temp_123
create table #temp_123(style varchar(100))
insert into #temp_123
exec(@sql)
----------------------
set @sql = 'select style as ' + '款号'
select @sql = @sql + ' , sum(case CONVERT(varchar(10),datepart(week,billdate)) when ''' + billdate + ''' then qty else 0 end) [' + dbo.GetWeekRange(billdate) + '周零售数量],'
+' sum(case CONVERT(varchar(10),datepart(week,billdate)) when ''' + billdate + ''' then Fmount else 0 end) [' + dbo.GetWeekRange(billdate) + '周成交金额]'
+', sum(case CONVERT(varchar(10),datepart(week,billdate)) when ''' + billdate + ''' then Fmount-qty*fob else 0 end) [' + dbo.GetWeekRange(billdate) + '周毛利]'
from (select distinct CONVERT(varchar(10),datepart(week,billdate)) as billdate from kaicheng_TBusRetailDT
where style in (select style from #temp_123)
set @temp = '##ddd'
-------------------增加
declare @sql_t nvarchar(1000)
set @sql_t = 'select style from ['+@temp+']'
declare @temp_123 table(style varchar(100))
insert into @temp_123
exec(@sql)
----------------------
set @sql = 'select style as ' + '款号'
select @sql = @sql + ' , sum(case CONVERT(varchar(10),datepart(week,billdate)) when ''' + billdate + ''' then qty else 0 end) [' + dbo.GetWeekRange(billdate) + '周零售数量],'
+' sum(case CONVERT(varchar(10),datepart(week,billdate)) when ''' + billdate + ''' then Fmount else 0 end) [' + dbo.GetWeekRange(billdate) + '周成交金额]'
+', sum(case CONVERT(varchar(10),datepart(week,billdate)) when ''' + billdate + ''' then Fmount-qty*fob else 0 end) [' + dbo.GetWeekRange(billdate) + '周毛利]'
from (select distinct CONVERT(varchar(10),datepart(week,billdate)) as billdate from kaicheng_TBusRetailDT
where style in (select style from @temp_123)
DECLARE @a VARCHAR(10)
SET @a='#t'
declare @sql varchar(max)
SELECT @sql='select * into '+@a+' from sys.sysobjects'
EXEC (@sql)
这样试试,不过EXEC完了之后临时表就会消失,所以如果你后续还需要用的话,就不要用临时表