Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 April 8th, 2004, 04:30 AM
Registered User
 
Join Date: Apr 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Setting output of Execute command to a @var

Hi,

I need to set the output of a query to a variable. What I want to do is to get a count of a select top query. The number of records I want to return is a variable so, as far as I'm aware, I need to create a query string and use the Execute command. This is how far I've got:

Set @sqlStatement = 'select TOP ' + convert(varchar, @numStatements) + ' * from [progressPDP].[dbo].[statementLabels] ORDER BY [labelIndex]'

Execute (@sqlStatement)

In pseudo code I think what I'm looking for is something like this:

Set @sqlStatement = 'select TOP ' + convert(varchar, @numStatements) + ' Count(*) from [progressPDP].[dbo].[statementLabels] ORDER BY [labelIndex]'

@theCount = Execute (@sqlStatement)

I then want to execute different queries depending on the value of @theCount.

Any help would be greatly appreciated.

Cheers,

Andy

 
Old April 8th, 2004, 10:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Andy,

I am little confused with your requirement. As you say

Quote:
quote:What I want to do is to get a count of a select top query. The number of records I want to return is a variable so, as far as I'm aware
Select TOP 10 * from TESTTABLE order by Col1

Returns the top 10 records in the ascending order of the values present on COL1. I dont think you would get a count of records, as you had mentioned.

Quote:
quote:Set @sqlStatement = 'select TOP ' + convert(varchar, @numStatements) + ' Count(*) from [progressPDP].[dbo].[statementLabels] ORDER BY [labelIndex]'

@theCount = Execute (@sqlStatement)
An OUTPUTPARAMETER can hold a single values in it. Something like a count/value of a column based on sime criteria. It cannot hold a set of rows in it, as the code/pseudocode that you posted would return set of rows with two columns in its result, which cannot be stored in an output parameter.

May be you may want to re-post your requirement in an understandable way, so as it guide you in the right path.

Cheers!

-Vijay G





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I use a global var as an output file name pmcquirk SQL Server DTS 2 May 5th, 2006 03:34 AM
connection.execute and command.execute difference vinod_yadav1919 Classic ASP Databases 0 September 28th, 2005 06:34 AM
conn.execute,command.execute or rs.open vinod_yadav1919 Crystal Reports 0 January 3rd, 2005 10:26 AM
execute external command in asp s80ts0465 Classic ASP Basics 4 August 3rd, 2004 01:55 PM
Execute command lines from ASP???? MAtkins Classic ASP Basics 4 December 4th, 2003 11:53 AM





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