473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

use of variables - why doesnt htis work

I am using SQL Server 2000

This doesn't work :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP @topnum *
FROM tblMyTable

I also tried :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP (SELECT @topnum) *
FROM tblMyTable

but this does :-

SELECT TOP 2 *
FROM tblMyTable
Why? How can I use a variable to control the number of rows returned
by the TOP key word?

I need to avoid using the ROWCOUNT setting otherwise I may have to
resort to a CURSOR god forbid!
Duncan
Jul 20 '05 #1
3 3227
On 24 Apr 2004 12:26:36 -0700, duncan wrote:
I am using SQL Server 2000

This doesn't work :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP @topnum *
FROM tblMyTable

I also tried :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP (SELECT @topnum) *
FROM tblMyTable

but this does :-

SELECT TOP 2 *
FROM tblMyTable
Why? How can I use a variable to control the number of rows returned
by the TOP key word?
You can't. The TOP clause accepts only an integer constant.

I need to avoid using the ROWCOUNT setting
Why? What's wrong with using ROWCOUNT?

I admit that it's proprietary syntax, not portable to other DB's. But
so is the TOP clause, so what's the difference?
otherwise I may have to
resort to a CURSOR god forbid!


No you don't. Even without TOP *and* without ROWCOUNT, there still are
other options available. The only difference is that the other options
do require you to properly specify what you want.

Lets' get back to your example:

SELECT TOP 2 *
FROM MyTable

So, you're asking for the "first" 2 rows from MyTable. But "first" by
what definition? Tables in the relational model are by definition an
*UN*ordered collection of rows - so unless you specify an order by
clause, the results of this query are undefined. The only thing you
can rely on is that you'll get two rows from MyTable - but which rows?

As soon as you specify the order, there's also an ANSI-standard
alternative without TOP or ROWCOUNT - and one that will work with a
variable. E.g. the following

SELECT TOP 2 *
FROM MyTable
ORDER BY MySortColumn

is equivalent to

DECLARE @topnum int
SET @topnum = 2
SELECT *
FROM MyTable AS MT1
WHERE (SELECT COUNT(*)
FROM MyTable AS MT2
WHERE MT2.MySortColumn < MT1.MySortColumn) < @topnum

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
When using variables in a statement you must encapsulate the entire
statement in another variable itself and then execute that statement.
For example:

declare @topnum as varchar, @command as varchar(1000)
set @topnum = 2

set @command = 'select top ' + @topnum + ' * from tblMyTable'

exec (@command)


Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<se********************************@4ax.com>. ..
On 24 Apr 2004 12:26:36 -0700, duncan wrote:
I am using SQL Server 2000

This doesn't work :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP @topnum *
FROM tblMyTable

I also tried :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP (SELECT @topnum) *
FROM tblMyTable

but this does :-

SELECT TOP 2 *
FROM tblMyTable
Why? How can I use a variable to control the number of rows returned
by the TOP key word?


You can't. The TOP clause accepts only an integer constant.

I need to avoid using the ROWCOUNT setting


Why? What's wrong with using ROWCOUNT?

I admit that it's proprietary syntax, not portable to other DB's. But
so is the TOP clause, so what's the difference?
otherwise I may have to
resort to a CURSOR god forbid!


No you don't. Even without TOP *and* without ROWCOUNT, there still are
other options available. The only difference is that the other options
do require you to properly specify what you want.

Lets' get back to your example:

SELECT TOP 2 *
FROM MyTable

So, you're asking for the "first" 2 rows from MyTable. But "first" by
what definition? Tables in the relational model are by definition an
*UN*ordered collection of rows - so unless you specify an order by
clause, the results of this query are undefined. The only thing you
can rely on is that you'll get two rows from MyTable - but which rows?

As soon as you specify the order, there's also an ANSI-standard
alternative without TOP or ROWCOUNT - and one that will work with a
variable. E.g. the following

SELECT TOP 2 *
FROM MyTable
ORDER BY MySortColumn

is equivalent to

DECLARE @topnum int
SET @topnum = 2
SELECT *
FROM MyTable AS MT1
WHERE (SELECT COUNT(*)
FROM MyTable AS MT2
WHERE MT2.MySortColumn < MT1.MySortColumn) < @topnum

Best, Hugo

Jul 20 '05 #3
Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<se********************************@4ax.com>. ..
On 24 Apr 2004 12:26:36 -0700, duncan wrote:
I am using SQL Server 2000

This doesn't work :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP @topnum *
FROM tblMyTable

I also tried :-

DECLARE @topnum AS INT
SET @topnum = 2

SELECT TOP (SELECT @topnum) *
FROM tblMyTable

but this does :-

SELECT TOP 2 *
FROM tblMyTable
Why? How can I use a variable to control the number of rows returned
by the TOP key word?


You can't. The TOP clause accepts only an integer constant.

I need to avoid using the ROWCOUNT setting


Why? What's wrong with using ROWCOUNT?

I admit that it's proprietary syntax, not portable to other DB's. But
so is the TOP clause, so what's the difference?
otherwise I may have to
resort to a CURSOR god forbid!


No you don't. Even without TOP *and* without ROWCOUNT, there still are
other options available. The only difference is that the other options
do require you to properly specify what you want.

Lets' get back to your example:

SELECT TOP 2 *
FROM MyTable

So, you're asking for the "first" 2 rows from MyTable. But "first" by
what definition? Tables in the relational model are by definition an
*UN*ordered collection of rows - so unless you specify an order by
clause, the results of this query are undefined. The only thing you
can rely on is that you'll get two rows from MyTable - but which rows?

As soon as you specify the order, there's also an ANSI-standard
alternative without TOP or ROWCOUNT - and one that will work with a
variable. E.g. the following

SELECT TOP 2 *
FROM MyTable
ORDER BY MySortColumn

is equivalent to

DECLARE @topnum int
SET @topnum = 2
SELECT *
FROM MyTable AS MT1
WHERE (SELECT COUNT(*)
FROM MyTable AS MT2
WHERE MT2.MySortColumn < MT1.MySortColumn) < @topnum

Best, Hugo

Try this:

declare @topnum as varchar, @command as varchar(1000)
set @topnum = 2

set @command = 'select top ' + @topnum + ' * from updates'

exec (@command)
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: ZoombyWoof | last post by:
Hi. Im very new to php, and I have a problem here that I cant find the solution for. I have problems with session variables, I want some variables to maintain their value between different php...
3
by: duncan | last post by:
I am using SQL Server 2000 This doesn't work :- DECLARE @topnum AS INT SET @topnum = 2 SELECT TOP @topnum * FROM tblMyTable
12
by: zig | last post by:
I've posted this on alt.comp.lang.coldfusion, but is predominantly a javascript problem: I have a CF query which returns several rows of records. I wanted to have a checkbox on each record of...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
6
by: Yahoo | last post by:
I am trying to determine if an object inherits a given interface at runtime and cant figure out how to do it. if we knew the interface type at design time... if (x is typeof(InterfaceName)) ...
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
6
by: RFS666 | last post by:
Hello, After I posted yesterday "using C# class in jscript", I have a new problem: I have a C# class - DBResult - that contains (and other variables) a string array (and other variables), that...
1
by: bizt | last post by:
Hi, I am trying to register session variables in the following way: $_SESSION = 'Burnsy'; When i set them and then output the following on the same script: echo $_SESSION
4
by: Krishna | last post by:
Environment variable set up is the most confusing part for me all the time. Please help me with the following questions: When I install python in a new system, I will go to environment variables...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.