stored procedures - Syntax error when assign a variable for top clause in sql server -


i need top 10, top 100 records table. need assign value top clause such 10 or 100 in variable.

but when give below, gives syntax error "incorrect syntax near '@numberofrecords'.".

declare @numberofrecords int select @numberofrecords = configvalue tblconfigitems (nolock) configname = 'toprecords'  select top @numberofrecords [id],typeid, messagetype, operationdate notifytbl (nolock) status in ('1', '2') 

how achieve this?

avoid dinamic sql. use this:

select top (@numberofrecords) [id],typeid, messagetype, operationdate notifytbl (nolock) status in ('1', '2') 

you need @ least sql 2005 have working. yes () trick.


Comments

Popular posts from this blog

java - Unable to make sub reports with Jasper -

scala - play framework: Modules were resolved with conflicting cross-version suffixes -

Passing Variables from AngelScript to C++ -