Click to See Complete Forum and Search --> : Sivakumar.kr


aviskumar
July 10th, 2009, 06:45 AM
The following statement works fine.

EXECUTE sp_executesql N'SELECT TOP 10 * FROM Table1'

How should i pass the value for 'Top n' where 'n' will vary.

ie., 'SELECT TOP ' + '2' + ' * FROM Table1'

Thanks in Advance.

Regards,
Sivakumar.kr

ComITSolutions
July 11th, 2009, 06:56 AM
Declare @Qry Nvarchar(1000)
Declare @TopRows Int
Set @TopRows=2
Set QRy =N'SELECT TOP ' + Cast( @TopRows as nvarchar) + ' * FROM Table1'

EXECUTE sp_executesql @Qry

Shuja Ali
July 11th, 2009, 07:33 AM
I usually don't like dynamic SQL. You could use a direct SQL like this Declare @TopRows Int
Set @TopRows=2
SELECT TOP ( @TopRows ) * FROM Table1

aviskumar
July 20th, 2009, 04:46 AM
I have got through the answer.

The following Statement works fine.

Declare @str as varchar(100)
Set @str='Select Top ' + @Nos + ' * from sta_qualificationMaster'
EXEC(@str)

With regards,
Sivakumar.kr.