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 |
|
tclose
Starting Member
24 Posts |
Posted - 2010-04-27 : 11:15:28
|
| I created a query that is suppose to take 11 unrelated tables, put them in a temporary table and then tally the sum of balances based off of the first 2 digits in the Account. When I run this as a regular query in SQL Server 2008 it works fine. However, when I use it as a stored procedure in a Visual Studio Report - it only displays the first set of results and doesn't display the balance sum. It should find many accounts and their corresponding balance sum. I tried using it as a regular query with the same results. It's as if the loop stops after finding the first set. Any ideas on this, thanks.Declare @i tinyint = 0Declare @j tinyint = 1Declare @sum money = 0CREATE TABLE #TempTable (Account int NOT NULL,Description varchar(2000),Product varchar(2000),Balance money, CONSTRAINT [pk_account] PRIMARY KEY(Account))while (@i < 11)BEGINIF @i = 0INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM AssetsELSE IF @i = 1INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM FreightELSE IF @i = 2INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM PurchasesELSE IF @i = 3INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM SalesELSE IF @i = 4INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM ExpensesELSE IF @i = 5INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM LiabilitiesELSE IF @i = 6INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM Inventory ELSE IF @i = 7INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM ProfitELSE IF @i = 8INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM PayrollELSE IF @i = 9INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM ReturnsELSE IF @i = 10INSERT INTO #TempTable(Account,Description,Product,Balance) SELECT Account,Description,Product,Balance FROM VendorsSET @i += 1ENDWHILE (@j < 62)BEGINSELECT @sum = (SELECT SUM(Balance) FROM #TempTable WHERE LEFT(Account,2) = @j AND Balance IS NOT NULL)IF @sum != 0SELECT Account,Description,Product,Balance FROM #TempTable WHERE LEFT(Account,2) = @j AND Balance IS NOT NULL SET @sum = (SELECT SUM(Balance) FROM #TempTable WHERE LEFT(Account,2) = @j AND Balance IS NOT NULL)IF @sum > 0 SELECT SUM(Balance) as [SubTotal] FROM #TempTable WHERE LEFT(Account,2) = @j AND Balance IS NOT NULLSET @j += 1SET @sum = 0ENDDROP TABLE #TempTableEND |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 11:20:48
|
| its how reports work. they take only first result set. why not combine everything into one result along with a derived category column and then filter inside report based on category------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tclose
Starting Member
24 Posts |
Posted - 2010-04-27 : 11:38:32
|
quote: Originally posted by visakh16 its how reports work. they take only first result set. why not combine everything into one result along with a derived category column and then filter inside report based on category------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I obviously don't understand how report queries differ from other queries. How would I combine everything into one result. |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-27 : 11:42:40
|
quote: Originally posted by tcloseI obviously don't understand how report queries differ from other queries. How would I combine everything into one result.
Redesign your stored proc so that it only returns a single result set. You could insert the results of each select statement into a temp table, and select the temp table at the end of the proc.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 11:46:46
|
quote: Originally posted by tclose
quote: Originally posted by visakh16 its how reports work. they take only first result set. why not combine everything into one result along with a derived category column and then filter inside report based on category------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I obviously don't understand how report queries differ from other queries. How would I combine everything into one result.
using UNION or UNION ALL . thats all i can say unless i see their resultsets. why not post sample resultset from each query and then explain what you want in report------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tclose
Starting Member
24 Posts |
Posted - 2010-04-27 : 12:21:59
|
quote: Originally posted by DBA in the making
quote: Originally posted by tcloseI obviously don't understand how report queries differ from other queries. How would I combine everything into one result.
Redesign your stored proc so that it only returns a single result set. You could insert the results of each select statement into a temp table, and select the temp table at the end of the proc.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee.
I think I get what you're saying. How would I get the subTotal column into the table though. Here is a sample of how the report should look:Account|Description|Product|Balance-----------------------------------_10234 farm fresh Eggs 225.0010783 Whole Milk 175.00subTotal----------400.00Account|Description|Product|Balance-----------------------------------_12034 Jimmy Dean Bacon 130.0012913 Low Fat Bologna 155.00subTotal----------285.00Account|Description|Product|Balance-----------------------------------_13007 baby Carrots 314.0013270 Red Apples 110.00subTotal----------424.00 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:26:45
|
| can you tell on what basis you determine which rows are to be grouped?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tclose
Starting Member
24 Posts |
Posted - 2010-04-27 : 12:32:25
|
quote: Originally posted by visakh16 can you tell on what basis you determine which rows are to be grouped?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
They will be grouped by the first two digits in the account number like so:102341094510038subTotal110341187011466subTotal140031403214990subTotal |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:35:52
|
| then what you need is to bring only detail data in your procedure and in your report group on left(accnt,2) and then on detail portion drag and drop a table with reqd fields and on group footer add expression =SUM(Fields!Balance.Value)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tclose
Starting Member
24 Posts |
Posted - 2010-04-27 : 13:19:41
|
quote: Originally posted by visakh16 then what you need is to bring only detail data in your procedure and in your report group on left(accnt,2) and then on detail portion drag and drop a table with reqd fields and on group footer add expression =SUM(Fields!Balance.Value)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I understand the Group part, but not the drag and drop a table part. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 13:48:16
|
| that means you'll have a table within a table. first table you apply group by and in its group detail part you drag and drop a new table with reqd fields. in main tables group footer you show the total------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|