Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about sp_executesql and the TOP argument

Status
Not open for further replies.

djj55

Programmer
Feb 6, 2006
1,761
US
I would like to build a sql statement that has a variable top command
Code:
SELECT TOP @passedvalue * FROM mytable
that will go into a sp_executesql statement. Is this possible?

Something like
Code:
create procedure usp_myproc 
( @passedvalue INT)
AS 
DECLARE @strsql NVARCHAR(1000)
DECLARE @params NVARCHAR(1000)
SET @strsql = N'SELECT TOP @pv column1, colum5 FROM mytable'
SET @params = N'@pv INT'
EXECUTE sp_executesql @strsql, @params, @pv = @passedvalue

Thank you,

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Found the problem I forgot to use the parenthesis around the TOP value.
Code:
SET @strsql = N'SELECT TOP (@pv) column1, colum5 FROM mytable'

Thanks anyways,

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top