Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
DavidD
Yak Posting Veteran
73 Posts |
Posted - 2004-01-15 : 20:45:32
|
| Dear all, I am getting the error message:Server: Msg 4414, Level 16, State 1, Procedure prc_MTAllMATSales, Line 6Could not allocate ancillary table for view or function resolution. Themaximum number of tables in a query (256) was exceeded. when I try to run a proc, the proc itself uses only one view, which is based on 5 tables. The proc uses correlated sub-queries so I am wondering if these create temporary tables? The proc also works when I break it down and it compiles fine. Any ideas anyone? I have enclosed the proc, it is large, but shouldn't kill a production server!!Thanks in advance Davidalter proc prc_MTAllMATSales @product varchar(50), @startdate datetime,@enddate datetimeas--first the normal single productsif @product in ('COV2MG', 'COV4MG', 'COVPLU', 'AERODI', 'DIAMIC', 'DIAMMR','NATR30', 'GLYADE', 'MUPHOR', 'NATR30', 'NATRSR', 'NXSR90', 'NATR90')beginselect g1.SalesDate, g1.NSW, [NSW MAT] = (select sum(g2.NSW) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [NSW MMA] = (select avg(g2.NSW) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [NSW MQT] = (select sum(g2.NSW) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -3, g1.SalesDate) and g2.Product = @product), g1.Vic, [Vic MAT] = (select sum(g2.Vic) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [VIC MMA] = (select avg(g2.Vic) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), g1.Qld, [Qld MAT] = (select sum(g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [Qld MMA] = (select avg(g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), g1.Sa, [Sa MAT] = (select sum(g2.Sa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [Sa MMA] = (select avg(g2.Sa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), g1.Wa, [Wa MAT] = (select sum(g2.Wa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [Wa MMA] = (select avg(g2.Wa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), g1.Tas, [Tas MAT] = (select sum(g2.Tas) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [Tas MMA] = (select avg(g2.Tas) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), g1.NZ, [NZ MAT] = (select sum(g2.NZ) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [NZ MMA] = (select avg(g2.NZ) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product), [MQT] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -3, g1.SalesDate) and g2.Product = @product), [MAT Growth] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product)/ (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -23, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product = @product) * 100 - 100, [MQT Growth] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -3, g1.SalesDate) and g2.Product = @product)/ (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -14, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product = @product) * 100 - 100, [MAT Uplift] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product = @product) - (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -23, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product = @product), [MQT Uplift] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -3, g1.SalesDate) and g2.Product = @product) - (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -14, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product = @product)from vw_AllProducts g1where Product = @productand SalesDate between @startdate and @enddateorder by g1.SalesDateend--now the combination productselse if @product in ('Total Cov', 'Total Nat', 'Total Dim')beginif @product = 'Total Cov' set @product = '(''COV2MG'', ''COV4MG'',''COVPLU'')'else if @product = 'Total Nat' set @product = '(''NATR30'', ''NATRSR'')'else if @product = 'Total Dim' set @product = '(''DIAMIC'', ''DIAMMR'')'declare @sql varchar(5000)set @sql = 'select g1.SalesDate, sum(g1.NSW) as NSW, [NSW MAT] = (select sum(g2.NSW) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [NSW MMA] = (select avg(g2.NSW) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), sum(g1.Vic) as Vic, [Vic MAT] = (select sum(g2.Vic) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [Vic MMA] = (select avg(g2.Vic) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), sum(g1.Qld) as Qld, [Qld MAT] = (select sum(g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [Qld MMA] = (select avg(g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), sum(g1.Sa) as Sa, [Sa MAT] = (select sum(g2.Sa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [Sa MMA] = (select avg(g2.Sa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), sum(g1.Wa) as Wa, [Wa MAT] = (select sum(g2.Wa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [Wa MMA] = (select avg(g2.Wa) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), sum(g1.Tas) as Tas, [Tas MAT] = (select sum(g2.Tas) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [Tas MMA] = (select avg(g2.Tas) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), sum(g1.NZ) as NZ, [NZ MAT] = (select sum(g2.NZ) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [NZ MMA] = (select avg(g2.NZ) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + '), [MQT] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -3, g1.SalesDate) and g2.Product in ' + @product + '), [MAT Growth] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + ')/ (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -23, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product in ' + @product + ') * 100 - 100, [MQT Growth] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -3, g1.SalesDate) and g2.Product in ' + @product + ')/ (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -14, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product in ' + @product + ') * 100 - 100, [MAT Uplift] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -12, g1.SalesDate) and g2.Product in ' + @product + ') - (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -23, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product in ' + @product + '), [MQT Uplift] = (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate > dateadd(mm, -3, g1.SalesDate) and g2.Product in ' + @product + ') - (select sum(g2.NSW + g2.Vic + g2.Sa + g2.Wa + g2.Tas + g2.Qld) from vw_AllProducts g2 where g2.SalesDate <= g1.SalesDate and g2.SalesDate between dateadd(mm, -14, g1.SalesDate) and dateadd(mm,-12, g1.SalesDate) and g2.Product in ' + @product + ')from vw_AllProducts g1where Product in ' + @product + 'and SalesDate between ''' + cast(@startdate as varchar(100)) + ''' and ''' +cast(@enddate as varchar(100)) + '''group by g1.SalesDateorder by g1.SalesDate'--print @sqlexec(@sql)end |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-01-15 : 22:16:33
|
| Frankly I can't even BEGIN to figure out what this query is supposed to return, but yes, you are using way too many subqueries, and yes, they do use worktables to generate intermediate results if the optimizer needs them. As far as killing a production server, I'm amazed that it literally isn't pouring smoke out at you after running this.Seriously, this is not the way you should be writing SQL, and I think you would be better off dumping this entirely and starting over. The CASE expression (see Books Online) will probably do all the work that this query does without a single subquery, but in order for us to help you'll need to explain what the results are supposed to look like. Right now the only thing I can make sense of is that almost all of the subqueries are identical, and that I don't have any idea why the dynamic SQL was used. |
 |
|
|
DavidD
Yak Posting Veteran
73 Posts |
Posted - 2004-01-15 : 23:08:18
|
| Thanks for the reply Rob, The query is used to generate results for an MAT (Moving Annual Total) financial report, I have solved the problem by breaking up the results into temp tables and then joining them - actually making more work for the server, but obviously not having as many dynamic tables being opened to do the work. I don't know of another way to calculate MATs other than using correlated sub-queries, and 17 in a report is no NASA calculation - I still think sql Server should have coped.RegardsDavid |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2004-01-15 : 23:21:24
|
quote: is no NASA calculation - I still think Sql Server should have coped.
If you turned this SQL query into its equivalent relational alegbra it would look like a NASA calculation!DavidM"SQL-3 is an abomination.." |
 |
|
|
|
|
|
|
|