DECLARE @sql nvarchar(MAX), @dbname sysname = 'tempdb' SELECT @sql = N' SELECT iif((InlagdSillSum > IpohCoffeeSum), abs(InlagdSillSum - IpohCoffeeSum), 0) as InlagdSill, iif((InlagdSillSum < IpohCoffeeSum), abs(InlagdSillSum - IpohCoffeeSum), 0) as IpohCoffee , iif((EqInlagdSillSum > EqIpohCoffeeSum), abs(EqInlagdSillSum - EqIpohCoffeeSum), 0) as EqInlagdSill, iif((EqInlagdSillSum < EqIpohCoffeeSum), abs(EqInlagdSillSum - EqIpohCoffeeSum), 0) as EqIpohCoffee, AniseedSyrup, BostonCrabMe, CamembertPier, CarnarvonTige, Chai, Chang, Chartreusever, ChefAntonsC, ChefAntonsG, Chocolade, CôtedeBlaye, EscargotsdeB, FiloMix, Flotemysost, Geitost, GenenShouyu, Gnocchidinon, GorgonzolaTel, GrandmasBoys, Gravadlax, GuaranáFantás, Gudbrandsdalso,GumbärGummibä, GustafsKnäck,Ikura, Lakkalikööri, LaughingLumbe, NuNuCaNußNou, LonglifeTofu, LouisianaFier, LouisianaHot, ManjimupDried, MascarponeFab, Maxilaku, MishiKobeNik, Mozzarelladi, NordOstMatje, NorthwoodsCra, AliceMutton, OriginalFrank, OutbackLager, Pâtéchinois, Pavlova,PerthPasties,QuesoCabrales FROM (SELECT AniseedSyrup, BostonCrabMe, CamembertPier, CarnarvonTige, Chai, Chang, Chartreusever, ChefAntonsC, ChefAntonsG, Chocolade, CôtedeBlaye, EscargotsdeB, FiloMix, Flotemysost, Geitost, GenenShouyu, Gnocchidinon, GorgonzolaTel, GrandmasBoys, Gravadlax, GuaranáFantás, Gudbrandsdalso, NULL AS AliceMutton, '''' AS GumbärGummibä, '''' AS GustafsKnäck, '''' AS Ikura, SUM(InlagdSill) AS InlagdSillSum, SUM(IpohCoffee) AS IpohCoffeeSum, SUM(EqIpohCoffee) AS EqIpohCoffeeSum, SUM(EqInlagdSill) AS EqInlagdSillSum, '''' AS Lakkalikööri, ''Four Objects'' AS LaughingLumbe, '''' AS LonglifeTofu, '''' AS LouisianaFier, '''' AS LouisianaHot, '''' AS ManjimupDried, '''' AS MascarponeFab, '''' AS Maxilaku, '''' AS MishiKobeNik, '''' AS Mozzarelladi, '''' AS NordOstMatje, '''' AS NorthwoodsCra, ''Five Subjects'' AS NuNuCaNußNou, OriginalFrank, OutbackLager, Pâtéchinois, Pavlova, ''Nine and Six'' as PerthPasties, QuesoCabrales FROM ' + quotename(@dbname) + N'.dbo.View_XVUnitsProducts WHERE ((OpeningRec = 0) OR (OpeningRec IS NULL)) AND ((closingRec = 0) OR (closingRec IS NULL)) AND AliceMutton < ''2016/01/01'' AND Chai >= 33 AND Chai <= 43 AND BostonCrabMe >= 201 AND BostonCrabMe <= 203 GROUP BY AniseedSyrup, BostonCrabMe, CamembertPier, CarnarvonTige, Chai, Chang, Chartreusever, ChefAntonsC, ChefAntonsG, Chocolade, CôtedeBlaye, EscargotsdeB, FiloMix, Flotemysost, Geitost, GenenShouyu, Gnocchidinon, GorgonzolaTel, GrandmasBoys, Gravadlax, GuaranáFantás, Gudbrandsdalso, OriginalFrank, OutbackLager, Pâtéchinois, Pavlova, QuesoCabrales UNION ALL SELECT AniseedSyrup, BostonCrabMe, CamembertPier, CarnarvonTige, Chai, Chang, Chartreusever, ChefAntonsC, ChefAntonsG, Chocolade, CôtedeBlaye, EscargotsdeB, FiloMix, Flotemysost, Geitost, GenenShouyu, Gnocchidinon, GorgonzolaTel, GrandmasBoys, Gravadlax, GuaranáFantás, Gudbrandsdalso, AliceMutton, GumbärGummibä, GustafsKnäck, Ikura, InlagdSill, IpohCoffee, EqIpohCoffee, EqInlagdSill, Lakkalikööri, LaughingLumbe, LonglifeTofu, LouisianaFier, LouisianaHot, ManjimupDried, MascarponeFab, Maxilaku, MishiKobeNik, Mozzarelladi, NordOstMatje, NorthwoodsCra, NuNuCaNußNou, OriginalFrank, OutbackLager, Pâtéchinois, Pavlova,PerthPasties,QuesoCabrales FROM ' + quotename(@dbname) + N'.dbo.View_XVUnitsProducts WHERE ((OpeningRec = 0) OR (OpeningRec IS NULL)) AND ((closingRec = 0) OR (closingRec IS NULL)) AND AliceMutton >= ''2016/01/01'' AND AliceMutton <= ''2017/12/13'' AND Chai >= 33 AND Chai <= 43 AND BostonCrabMe >= 201 AND BostonCrabMe <= 203) AS XVUnitsTrxSafe' SELECT len(@sql), @sql EXEC sp_executesql @sql go