Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2005, 12:35 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Query Sum

I am trying to get the sum of two dynamic queries
That is:
Code:
EXEC('SELECT COUNT(id) as subtotal1...) + EXEC('SELECT COUNT(id) as subtotal2...)
If I assign to a variable, say @total, I get errors or NULLs. Is there a workaround?

Thanks in advance for your comments

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old March 29th, 2005, 02:57 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I couldn't get you code to work. How about trying this
declare @cnt1 int
declare @cnt2 int
declare @total int

select @cnt1 = (select count(*) from <table>)
select @cnt2 = (select count(*) from <table>)
select @total = @cnt1 + @cnt2
select @total

 
Old March 29th, 2005, 04:26 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That doesn't work with the dynamic aspect of it.
Code:
select @cnt1 = (EXEC('select count(*) from <table>)')
Gives an error, "Incorrect syntax near 'EXEC'"

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old March 29th, 2005, 04:28 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

i am getting the error too.. I am working on it.. let me know if you get it.. and i will do the same

 
Old March 30th, 2005, 11:08 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think this may be inefficient, but I gave this a try:
Code:
declare @total int
declare @tot1 int
declare @tot2 int

CREATE TABLE #table1 (tot int)
INSERT INTO #table1 EXEC('SELECT COUNT(C.id)...')

CREATE TABLE #table2 (tot int)
INSERT INTO #table2 EXEC('SELECT COUNT(C.id)...')

SELECT @tot1 = tot FROM #table1
SELECT @tot2 = tot FROM #table2

SELECT @total = @tot1 + @tot2
RETURN @total
This didn't give any errors, but I'm having trouble getting it to produce results. In fact, my query appears to hang on it. Hope it's a start.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sum Query rsm42 ASP.NET 1.0 and 1.1 Basics 2 April 11th, 2007 01:52 PM
Dynamic Query ninel SQL Server 2000 1 March 8th, 2005 01:18 AM
Dynamic Query zaman1111 Javascript 4 August 4th, 2004 02:16 PM
Dynamic query problem drex10 SQL Server 2000 2 April 6th, 2004 11:55 PM
ADO Dynamic Query - Please help rudidoku Access 1 July 23rd, 2003 06:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.