Yes it is possible to use a variable in a "top #" select statement
within
a stored procedure?

Create Proc TopNumber
@Number Int 
As
Exec ('Select Top ' + @Number + ' Lastname from Employees Order by
Lastname')

I hope this is what you are looking for

Larry Hough

-----Original Message-----
From: Andrew Zetterman [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, August 20, 2002 12:11 PM
To: ActiveServerPages
Subject: RE: dynamic "top *" sql statement?

How about this?

CREATE PROC sp_MyProc @AMaxRows int AS
BEGIN
  SET ROWCOUNT @AMaxRows
  select * from cust
END


-----Original Message-----
From: Scott [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, August 20, 2002 11:07 AM
To: ActiveServerPages
Subject: Re: dynamic "top *" sql statement?


You could pass your MaxRows variable into a proc and build your SQL
string.

CREATE PROC sp_MyProc @AMaxRows int AS
BEGIN
  set nocount on

  declare @sql varchar(255)
  set @sql = 'select top ' + cast(@AMaxRows) + ' * from cust'
  exec (@sql)

END

This is the only way I know of and have used. Tore? David?

hope that helps anyway


----- Original Message -----
From: "Tyler Brown" <[EMAIL PROTECTED]>
To: "ActiveServerPages" <[EMAIL PROTECTED]>
Sent: Wednesday, August 21, 2002 12:56 AM
Subject: dynamic "top *" sql statement?


: is it possible to use a variable in a "top #" select statement within
a
: stored procedure?
:
: When I try declaring and using a value I get an ado error: "incorrect
syntax
: near [variable name]"
:
: How does one get around this without dynamically creating the sql
string?
:
: ---
: You are currently subscribed to activeserverpages as:
[EMAIL PROTECTED]
: To unsubscribe send a blank email to
%%email.unsub%%
:



---
You are currently subscribed to activeserverpages as:
[EMAIL PROTECTED] To unsubscribe send a blank email to
%%email.unsub%%

---
You are currently subscribed to activeserverpages as:
[EMAIL PROTECTED]
To unsubscribe send a blank email to
%%email.unsub%%


---
You are currently subscribed to activeserverpages as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]

Reply via email to